Fixes javadoc builds
[smsapi-java.git] / src / main / java / pl / smsapi / proxy / ProxyNative.java
blob61fe7e0db74ca32953d5540459523c28ddf9f66e
1 package pl.smsapi.proxy;
3 import pl.smsapi.api.authenticationStrategy.AuthenticationStrategy;
4 import pl.smsapi.api.authenticationStrategy.BasicAuthenticationStrategy;
6 import java.io.*;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLEncoder;
11 import java.text.SimpleDateFormat;
12 import java.util.*;
14 public class ProxyNative implements Proxy {
16 private String baseUrl;
18 public ProxyNative(String url) {
20 this.baseUrl = url;
23 /**
24 * @deprecated
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);
38 /**
39 * Execute
40 * <p/>
41 * Disable ssl hostname verification
42 * <p/>
43 * <code>
44 * HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
45 * public boolean verify(StringUtils hostname, javax.net.ssl.SSLSession sslSession) {
46 * return true;
47 * }
48 * });
49 * </code>
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")) {
67 data.clear();
70 byte[] dataBytes;
72 if (files == null || files.isEmpty()) {
74 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
75 dataBytes = createDataStream(data);
76 } else {
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()));
94 String line;
95 while ((line = inputReader.readLine()) != null) {
96 response.append(line);
99 inputReader.close();
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;
118 } else {
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()) {
169 String fileHeader =
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];
178 int n;
179 while ((n = inputStream.read(buffer)) > 0) {
180 stream.write(buffer, 0, n);
183 inputStream.close();
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");