View Javadoc
1   package com.srv4pos.server.api.infrastructure.http;
2   
3   import com.srv4pos.server.api.infrastructure.ConnectionWriter;
4   import com.srv4pos.server.api.infrastructure.HttpMethod;
5   
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.net.HttpURLConnection;
9   import java.net.URL;
10  import java.util.Locale;
11  
12  /**
13   * Provides HTTP/HTTPS network layer for server api.
14   * <p/>
15   * <p>User: Kirill, Date: 13.09.13 13:35</p>
16   */
17  public class HttpUrlTrasport implements Transport {
18  
19      private HttpURLConnection connection;
20      private boolean isConnectionClose;
21  
22      public HttpUrlTrasport() {
23      }
24  
25      public HttpUrlTrasport(boolean isConnectionClose) {
26          this.isConnectionClose = isConnectionClose;
27      }
28  
29      public Response invoke(HttpMethod httpMethod, String uri, String authentication, String eTag, String mimeType,
30                             ConnectionWriter body, Locale acceptLanguage) {
31          try {
32              URL url = new URL(uri);
33  
34              if (connection != null) {
35                  closeConnection();
36              }
37  
38              connection = (HttpURLConnection) url.openConnection();
39              connection.setRequestMethod(httpMethod.name());
40              if (authentication != null) {
41                  connection.setRequestProperty(HttpHelper.AUTHORIZATION, authentication);
42              }
43              if (eTag != null) {
44                  if (httpMethod.equals(httpMethod.GET)) {
45                      connection.setRequestProperty("If-None-Match", eTag);
46                  } else {
47                      connection.setRequestProperty("If-Match", eTag);
48                  }
49              }
50              if (acceptLanguage != null) {
51                  connection.setRequestProperty("Accept-Language", HttpHelper.formatAcceptLanguageHeader(acceptLanguage));
52              }
53              boolean doOutput = HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod);
54              connection.addRequestProperty(HttpHelper.ACCEPT, mimeType);
55              connection.addRequestProperty(HttpHelper.CONTENT_TYPE, mimeType);
56              if (isConnectionClose) {
57                  connection.addRequestProperty(HttpHelper.CONNECTION, "close");
58              }
59              connection.setDoOutput(doOutput);
60              connection.setUseCaches(false);
61              connection.setInstanceFollowRedirects(false); // setting needed to switch of redirect if status code == 401
62              Integer contentLength = body.getContentLength();
63              if (doOutput && contentLength != null) {
64                  connection.setFixedLengthStreamingMode(contentLength);
65              }
66  
67              connection.connect();
68              if (doOutput) {
69                  body.write(connection.getOutputStream());
70              }
71              int responseCode = connection.getResponseCode();
72              InputStream inputStream = HttpHelper.isStatusCodeInSuccessfulClass(responseCode) ? connection.getInputStream() : connection.getErrorStream();
73              return new Response(
74                      inputStream,
75                      responseCode,
76                      connection.getContentEncoding(),
77                      connection.getContentType());
78          } catch (IOException e) {
79              throw new RuntimeException(e);
80          }
81      }
82  
83      public void closeConnection() {
84          if (connection != null) {
85              connection.disconnect();
86              connection = null;
87          }
88      }
89  }