Advertisement
Guest User

GarciaPL

a guest
Sep 3rd, 2012
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. public class LocationTelcoAPI {
  2.  
  3.     private RestTemplate restTemplate = new RestTemplate();
  4.     private String telco_location = "https://api.orange.pl/terminallocation/?msisdn={phone}";
  5.  
  6.     private void enableSSL() {
  7.         TrustManager[] trustAllCerts = new TrustManager[]{
  8.             new X509TrustManager() {
  9.                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  10.                     return null;
  11.                 }
  12.  
  13.                 public void checkClientTrusted(
  14.                         java.security.cert.X509Certificate[] certs, String authType) {
  15.                 }
  16.  
  17.                 public void checkServerTrusted(
  18.                         java.security.cert.X509Certificate[] certs, String authType) {
  19.                 }
  20.             }
  21.         };
  22.  
  23.         try {
  24.             SSLContext sc = SSLContext.getInstance("SSL");
  25.             sc.init(null, trustAllCerts, new java.security.SecureRandom());
  26.             HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  27.         } catch (Exception e) {
  28.         }
  29.     }
  30.  
  31.     public LocationResponse getLocation(String telephone_number) {
  32.  
  33.         enableSSL();
  34.  
  35.         String auth = TelcoAuth.USERNAME + ":" + TelcoAuth.PASSWORD;
  36.         byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
  37.         String authHeader = "Basic " + new String(encodedAuth);
  38.  
  39.         HttpHeaders headers = new HttpHeaders();
  40.         headers.set("Accept", "application/json");
  41.         headers.set("Authorization", authHeader);
  42.  
  43.         Map<String, String> variables = new HashMap<String, String>(1);
  44.         variables.put("phone", telephone_number);
  45.         ResponseEntity<LocationResponse> exchange = restTemplate.exchange(telco_location, HttpMethod.GET, new HttpEntity<String>(headers), LocationResponse.class, variables);
  46.  
  47.         System.out.println(exchange.getStatusCode());
  48.         System.out.println(exchange.getBody().getResult());
  49.         System.out.println(exchange.getBody().getLongitude());
  50.         System.out.println(exchange.getBody().getLatitude());
  51.         System.out.println(exchange.getBody().getAccuracy());
  52.         System.out.println(exchange.getBody().getAltitude());
  53.         System.out.println(exchange.getBody().getTimestamp());
  54.        
  55.         return exchange.getBody();
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement