/
DailyCDR.java
172 lines (146 loc) · 6.82 KB
/
DailyCDR.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.ipass.api.sample.billingreport;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* Sample Code to demonstrate how Billing Reports API can be used to retrieve
* + Daily CDR
* + Adjusted Daily CDR
*
* == PLEASE NOTE ==
* iPass provide this sample code just to demonstrate how our API works
* iPass will not provide any support for these samples
*
* iPass may change API attributes and resources, and our policies regarding
* access and use of APIs at any time, without advance notice. iPass will try
* its best to notify you of any modifications to the API or policies on Open Mobile Help.
*
* @author iPass.com (March, 2014)
* @version 1.0
*
*/
public class DailyCDR {
/**
* The following variables must be configured with your own account settings
*/
String BASE_URL = "https://openmobile.ipass.com";
String username = "johnsmith@acme.com";
String password = "changeme";
String companyId = "987654";
String filename = "ClientUserVersionReport.html";
String date = "2014-01-12";
HttpClient httpClient = null;
private X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
public void downloadDailyCDR() throws ClientProtocolException, IOException {
// Prepare the Daily CDR API call with the appropriate parameter
String reportURI = "/moservices/rest/api/ipass/" + companyId + "/mo/cdrReports/daily/cdrdaily?User-Agent=apiuser&date=" + date;
HttpGet reportGet = new HttpGet(BASE_URL + reportURI);
// Make the Monthly CDR API call to obtain the Monthly CDR report
HttpResponse reportGetResponse = httpClient.execute(reportGet);
HttpEntity httpEntity = reportGetResponse.getEntity();
// Process the HTTP Response to be output to the screen
// String reportOutput = EntityUtils.toString(httpEntity);
// System.out.println(reportOutput);
// Process the HTTP Response to be saved to C:\Temp\DailyCDR.csv
byte[] reportByteOutput = EntityUtils.toByteArray(httpEntity);
Path path = FileSystems.getDefault().getPath("C:\\Temp", "DailyCDR.csv");
Files.write(path, reportByteOutput, StandardOpenOption.CREATE);
}
public void downloadAdjustedDailyCDR() throws ClientProtocolException, IOException {
// Prepare the Daily CDR API call with the appropriate parameter
String reportURI = "/moservices/rest/api/ipass/" + companyId + "/mo/cdrReports/daily/adjcdrdaily?User-Agent=apiuser&date=" + date;
HttpGet reportGet = new HttpGet(BASE_URL + reportURI);
// Make the Monthly CDR API call to obtain the Monthly CDR report
HttpResponse reportGetResponse = httpClient.execute(reportGet);
HttpEntity httpEntity = reportGetResponse.getEntity();
// Process the HTTP Response to be output to the screen
// String reportOutput = EntityUtils.toString(httpEntity);
// System.out.println(reportOutput);
// Process the HTTP Response to be saved to C:\Temp\AdjustedDailyCDR.csv
byte[] reportByteOutput = EntityUtils.toByteArray(httpEntity);
Path path = FileSystems.getDefault().getPath("C:\\Temp", "AdjustedDailyCDR.csv");
Files.write(path, reportByteOutput, StandardOpenOption.CREATE);
}
public DailyCDR() throws NoSuchAlgorithmException, KeyManagementException, ClientProtocolException, IOException {
httpClient = new DefaultHttpClient();
// Set up necessary for SSL (HTTPS) stuff
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = httpClient.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));
httpClient = new DefaultHttpClient(ccm);
// Prepare the Login API Call with the appropriate parameters
String loginURI = "/moservices/rest/api/login?User-Agent=apiuser";
HttpPost loginPost = new HttpPost(BASE_URL + loginURI);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
loginPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Make the Login API call to authenticate
HttpResponse loginResponse = httpClient.execute(loginPost);
// Consume content so the HTTPClient can be re-used
loginResponse.getEntity().consumeContent();
// Get Cookies from the Login API call
CookieStore cookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
// Set cookies for future API calls
httpClient.getParams().setParameter("Cookie", cookies);
}
public static void main(String[] args) {
try {
DailyCDR atr = new DailyCDR();
atr.downloadDailyCDR();
atr.downloadAdjustedDailyCDR();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}