1 package pl
.smsapi
.proxy
;
3 import pl
.smsapi
.api
.authenticationStrategy
.AuthenticationStrategy
;
4 import pl
.smsapi
.api
.authenticationStrategy
.BasicAuthenticationStrategy
;
7 import java
.net
.HttpURLConnection
;
8 import java
.net
.MalformedURLException
;
10 import java
.net
.URLEncoder
;
11 import java
.text
.SimpleDateFormat
;
14 public class ProxyNative
implements Proxy
{
16 private String baseUrl
;
18 public ProxyNative(String url
) {
26 public String
execute(String endpoint
, Map
<String
, String
> data
, Map
<String
, InputStream
> files
) throws Exception
{
27 String username
= data
.get("username");
28 data
.remove("username");
30 String password
= data
.get("password");
31 data
.remove("password");
33 AuthenticationStrategy authenticationStrategy
= new BasicAuthenticationStrategy(username
, password
);
35 return execute(endpoint
, data
, files
, "POST", authenticationStrategy
);
41 * Disable ssl hostname verification
44 * HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
45 * public boolean verify(StringUtils hostname, javax.net.ssl.SSLSession sslSession) {
51 public String
execute(String endpoint
, Map
<String
, String
> data
, Map
<String
, InputStream
> files
, String httpMethod
, AuthenticationStrategy authenticationStrategy
) throws Exception
{
52 URL url
= createUrl(httpMethod
, endpoint
, data
);
53 HttpURLConnection connection
= (HttpURLConnection
) url
.openConnection();
54 connection
.setRequestProperty("User-Agent", "smsapi-lib/java " + System
.getProperty("os.name"));
55 connection
.setRequestProperty("Accept", "*");
56 connection
.setUseCaches(false);
57 connection
.setDoOutput(true);
58 connection
.setRequestMethod(httpMethod
);
60 String authenticationHeader
= authenticationStrategy
.getAuthenticationHeader();
62 if (authenticationHeader
!= null) {
63 connection
.setRequestProperty("Authorization", authenticationHeader
);
66 if (httpMethod
.equals("GET")) {
72 if (files
== null || files
.isEmpty()) {
74 connection
.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
75 dataBytes
= createDataStream(data
);
78 String boundary
= generateBoundary();
79 connection
.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary
);
80 dataBytes
= createMultipartDataStream(boundary
, data
, files
);
83 connection
.setRequestProperty("Content-Length", Integer
.toString(dataBytes
.length
));
85 if (dataBytes
.length
!= 0) {
86 connection
.getOutputStream().write(dataBytes
);
87 connection
.getOutputStream().flush();
88 connection
.getOutputStream().close();
91 StringBuilder response
= new StringBuilder();
92 BufferedReader inputReader
= new BufferedReader(new InputStreamReader(connection
.getInputStream()));
95 while ((line
= inputReader
.readLine()) != null) {
96 response
.append(line
);
101 return response
.toString();
104 private String
generateBoundary() {
105 Random generator
= new Random();
106 SimpleDateFormat format
= new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
107 return "SMSAPI-" + format
.format(new Date()) + generator
.nextInt() + "-boundary";
110 protected URL
createUrl(String httpMethod
, String endpoint
, Map
<String
, ?
> data
) throws UnsupportedEncodingException
, MalformedURLException
{
111 String urlString
= baseUrl
+ endpoint
;
113 if (httpMethod
.equals("GET") && !data
.isEmpty()) {
114 String queryString
= createQueryString(data
);
116 if (urlString
.contains("?")) {
117 urlString
= urlString
+ '&' + queryString
;
119 urlString
= urlString
+ '?' + queryString
;
123 return new URL(urlString
);
126 protected String
createQueryString(Map
<String
, ?
> data
) throws UnsupportedEncodingException
{
127 StringBuilder stringBuilder
= new StringBuilder();
129 Iterator
<?
extends Map
.Entry
<String
, ?
>> entryIterator
= data
.entrySet().iterator();
131 while (entryIterator
.hasNext()) {
133 Map
.Entry
<String
, ?
> entry
= entryIterator
.next();
135 String record
= encodeUrlParam(entry
.getKey()) + "=" + encodeUrlParam(entry
.getValue().toString());
136 stringBuilder
.append(record
);
138 if (entryIterator
.hasNext()) {
139 stringBuilder
.append('&');
143 return stringBuilder
.toString();
146 protected byte[] createDataStream(Map
<String
, ?
> data
) throws IOException
{
147 String queryString
= createQueryString(data
);
149 ByteArrayOutputStream stream
= new ByteArrayOutputStream();
151 stream
.write(queryString
.getBytes());
153 return stream
.toByteArray();
156 protected byte[] createMultipartDataStream(String boundary
, Map
<String
, ?
> data
, Map
<String
, InputStream
> files
) throws IOException
{
158 ByteArrayOutputStream stream
= new ByteArrayOutputStream();
160 for (Map
.Entry
<String
, ?
> entry
: data
.entrySet()) {
162 String paramHeader
= "\r\n--" + boundary
+ "\r\nContent-Disposition: form-data; name=\"" + entry
.getKey() + "\";\r\n\r\n";
163 stream
.write(paramHeader
.getBytes());
164 stream
.write(entry
.getValue().toString().getBytes());
167 for (Map
.Entry
<String
, InputStream
> entry
: files
.entrySet()) {
170 "\r\n--" + boundary
+
171 "\r\nContent-Disposition: form-data; name=\"" + entry
.getKey() + "\"; filename=\"" + entry
.getKey() + "\"" +
172 "\r\nContent-Type: application/octet-stream\r\n\r\n";
174 stream
.write(fileHeader
.getBytes());
176 InputStream inputStream
= entry
.getValue();
177 byte[] buffer
= new byte[4096];
179 while ((n
= inputStream
.read(buffer
)) > 0) {
180 stream
.write(buffer
, 0, n
);
186 byte[] footBytes
= ("\r\n--" + boundary
+ "--").getBytes();
187 stream
.write(footBytes
, 0, footBytes
.length
);
189 return stream
.toByteArray();
192 protected String
encodeUrlParam(String s
) throws UnsupportedEncodingException
{
193 return URLEncoder
.encode(s
, "UTF-8");