My Github GarciaPL

by GarciaPL on Thursday 6 September 2012


I just wanted to announce that I created my own github, where I am going to store my projects.  So,  stay tuned ;)




LaTeX simple table

by GarciaPL on Monday 3 September 2012


I would like to share with you an quick and I suppose painless piece of code to make your own table. Quite nice table i must add.

\begin{table}[ht]
\captionof{table}{Parametry zapytania dla Location SOAP API}
\begin{center}
\rowcolors{1}{lightgray}{}
%\begin{tabular}{|p{2cm}|p{12.8cm}|}
\begin{tabular}{| >{\centering\arraybackslash}m{2cm} | >{\centering\arraybackslash}m{12.8cm} |}
\hline
\multicolumn{1}{|c|}{\textbf{Parametr}} & \multicolumn{1}{c|}{\textbf{Opis wartości}} \\ \hline
address&Numer telefonu przeznaczony do zlokalizowania w formacie międzynarodowym (np. +48504500600) 
lub krajowym (np. 504500600)\\ \hline
\end{tabular}
\end{center}
\end{table}


Result :

LaTeX table
LaTeX table

Reference :
[1] Source Pastebin.com

LaTeX listing XML

by GarciaPL


I would like to share with LaTeX code which allow to listing in very nice way XML code.


\usepackage[font=normalsize,format=plain,labelfont={bf,normalsize},textfont={it,normalsize}]{caption}
\definecolor{lightgray}{gray}{0.9}
\usepackage{listings}
\usepackage{courier}

\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstset{
  basicstyle=\footnotesize\ttfamily, % Standardschrift
  %numbers=left, % Ort der Zeilennummern
  numberstyle=\tiny, % Stil der Zeilennummern
  %stepnumber=2, % Abstand zwischen den Zeilennummern
  numbersep=5pt, % Abstand der Nummern zum Text
  tabsize=2, % Groesse von Tabs
  extendedchars=true, %
  breaklines=true, % Zeilen werden Umgebrochen
  keywordstyle=\color{red},
    frame=b,
  % keywordstyle=[1]\textbf, % Stil der Keywords
  % keywordstyle=[2]\textbf, %
  % keywordstyle=[3]\textbf, %
  % keywordstyle=[4]\textbf, \sqrt{\sqrt{}} %
  stringstyle=\color{white}\ttfamily, % Farbe der String
  showspaces=false, % Leerzeichen anzeigen ?
  showtabs=false, % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=5pt,
  framexbottommargin=4pt,
  %backgroundcolor=\color{lightgray},
  showstringspaces=false % Leerzeichen in Strings anzeigen ?
}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}% list your attributes here
}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}



Using in LaTeX code :

\lstinputlisting[label=soaplocationresponse,language=XML,caption=Odpowiedź Location SOAP API]{locationsoapresponse.xml} 


Result :

LaTeX XML listing
LaTeX XML listing


Reference :
[1] Listing XML GarciaPL Pastebin.com
[2] Using XML listing Pastebin.com

Spring REST Template with SSL

by GarciaPL


Very fast guide how to use REST Template in Spring with SSL. It is just a few lines of code, but in some time it was very useful for me. This example is using Telco interfaces which allow to get subscriber location. Unfortunately this one is not available to the public.

public class LocationTelcoAPI {

    private RestTemplate restTemplate = new RestTemplate();
    private String telco_location = "https://api.orange.pl/terminallocation/?msisdn={phone}";

    private void enableSSL() {
        TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
            }
        };

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
        }
    }

    public LocationResponse getLocation(String telephone_number) {

        enableSSL();

        String auth = TelcoAuth.USERNAME + ":" + TelcoAuth.PASSWORD;
        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
        String authHeader = "Basic " + new String(encodedAuth);

        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", "application/json");
        headers.set("Authorization", authHeader);

        Map variables = new HashMap(1);
        variables.put("phone", telephone_number);
        ResponseEntity exchange = restTemplate.exchange(telco_location, HttpMethod.GET, new HttpEntity(headers), LocationResponse.class, variables);

        System.out.println(exchange.getStatusCode());
        System.out.println(exchange.getBody().getResult());
        System.out.println(exchange.getBody().getLongitude());
        System.out.println(exchange.getBody().getLatitude());
        System.out.println(exchange.getBody().getAccuracy());
        System.out.println(exchange.getBody().getAltitude());
        System.out.println(exchange.getBody().getTimestamp());
        
        return exchange.getBody();
    }
}

And class which help to deserialization XML :


@XmlRootElement(name = "response")
public class LocationResponse {

    @XmlElement
    private String result;
    @XmlElement
    private String latitude;
    @XmlElement
    private String longitude;
    @XmlElement
    private String altitude;
    @XmlElement
    private String accuracy;
    @XmlElement
    private String timestamp;

    public LocationResponse() {
    }

    public LocationResponse(String result, String latitude, String longitude, String altitude, String accuracy, String timestamp) {
        this.result = result;
        this.latitude = latitude;
        this.longitude = longitude;
        this.altitude = altitude;
        this.accuracy = accuracy;
        this.timestamp = timestamp;
    }

    public String getResult() {
        return result;
    }

    public String getLatitude() {
        return latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public String getAltitude() {
        return altitude;
    }

    public String getAccuracy() {
        return accuracy;
    }

    public String getTimestamp() {
        return timestamp;
    }

    @Override
    public String toString() {
        return "LocationResponse{" + "result=" + result + ", latitude=" + latitude + ", longitude=" + longitude + ", altitude=" + altitude + ", accuracy=" + accuracy + ", timestamp=" + timestamp + '}';
    }
}

UPDATE According to Abhijit comment, we can use ClientHttpRequestFactory used as a parameter in RestTemplate constructor. Just implement a ClientHttpRequestFactory and override the prepareConnection method to achieve the same effect as above, but with limiting customers of your system instead of sharing it globally with everyone [3].


Reference :
[1] Source Pastebin Main Class
[2] Source Pastebin XML Deserialization
[3] ClientHttpRequestFactory Example

JTS Topology Suite Point in Polygon

by GarciaPL


I would like to show you how easily calculate if an object i mean some point is inside in some king of polygon. Below algorithm is very useful for determining points in some area for example in Google Maps. All what you need to possess is set of coordinates of this area and point.

public boolean sprawdzObiektwObszarze(ArrayList obszar, Coordinate abonent) {
        final GeometryFactory gf = new GeometryFactory();
        LinearRing linearring = new LinearRing(new CoordinateArraySequence(obszar.toArray(newCoordinate[obszar.size()])), gf);
        final Polygon polygon = gf.createPolygon(linearring, null);
        final Point point = gf.createPoint(abonent);
        return point.within(polygon);
}

Function return true if point is in polygon or false if it is not.

PS. You must remember that first and last point of ArrayList<Coordinate> should be the same point to close the area.


Reference :
[1] JTS Topology Suite SourceForge.net
[2] Source Pastebin.com