Removing unused methods and parameters. docker_socket_only
authorPetr Hejl <phejl@netbeans.org>
Tue, 02 Feb 2016 11:34:56 +0100
branchdocker_socket_only
changeset 3071032e3628127b31
parent 307102 7a0af2511d3f
child 307104 316ca9a8eee7
Removing unused methods and parameters.
docker.api/src/org/netbeans/modules/docker/HttpUtils.java
docker.api/src/org/netbeans/modules/docker/api/DockerAction.java
     1.1 --- a/docker.api/src/org/netbeans/modules/docker/HttpUtils.java	Tue Feb 02 11:16:32 2016 +0100
     1.2 +++ b/docker.api/src/org/netbeans/modules/docker/HttpUtils.java	Tue Feb 02 11:34:56 2016 +0100
     1.3 @@ -48,7 +48,6 @@
     1.4  import java.io.InputStream;
     1.5  import java.io.OutputStream;
     1.6  import java.io.UnsupportedEncodingException;
     1.7 -import java.net.HttpURLConnection;
     1.8  import java.net.URLEncoder;
     1.9  import java.nio.charset.Charset;
    1.10  import java.nio.charset.UnsupportedCharsetException;
    1.11 @@ -109,21 +108,6 @@
    1.12          return new String(content, encoding);
    1.13      }
    1.14  
    1.15 -    public static String readError(HttpURLConnection conn) throws IOException {
    1.16 -        InputStream err = conn.getErrorStream();
    1.17 -        if (err == null || err.available() <= 0) {
    1.18 -            return null;
    1.19 -        }
    1.20 -        Charset encoding = getCharset(conn.getContentType());
    1.21 -        ByteArrayOutputStream bos = new ByteArrayOutputStream(200);
    1.22 -        byte[] content = new byte[200];
    1.23 -        int length;
    1.24 -        while((length = err.read(content)) != -1) {
    1.25 -            bos.write(content, 0, length);
    1.26 -        }
    1.27 -        return bos.toString(encoding.name());
    1.28 -    }
    1.29 -
    1.30      public static InputStream getResponseStream(InputStream is, Response response, boolean allowSocketStream) throws IOException {
    1.31          if (isChunked(response.getHeaders())) {
    1.32              return new ChunkedInputStream(new BufferedInputStream(is));
    1.33 @@ -183,10 +167,6 @@
    1.34          return getCharset(response.getHeaders().get("CONTENT-TYPE")); // NOI18N
    1.35      }
    1.36  
    1.37 -    public static Charset getCharset(HttpURLConnection connection) {
    1.38 -        return getCharset(connection.getContentType());
    1.39 -    }
    1.40 -
    1.41      public static String encodeParameter(String value) throws UnsupportedEncodingException {
    1.42          return URLEncoder.encode(value, "UTF-8");
    1.43      }
    1.44 @@ -218,20 +198,6 @@
    1.45          }
    1.46      }
    1.47  
    1.48 -    public static void configureHeaders(HttpURLConnection conn, Map<String, String> defaultHeaders,
    1.49 -            Pair<String, String>... headers) {
    1.50 -
    1.51 -        for (Map.Entry<String, String> e : defaultHeaders.entrySet()) {
    1.52 -            conn.setRequestProperty(e.getKey(), e.getValue());
    1.53 -        }
    1.54 -        for (Pair<String, String> h : headers) {
    1.55 -            if (h == null) {
    1.56 -                continue;
    1.57 -            }
    1.58 -            conn.setRequestProperty(h.first(), h.second());
    1.59 -        }
    1.60 -    }
    1.61 -
    1.62      static String readResponseLine(InputStream is) throws IOException {
    1.63          ByteArrayOutputStream bos = new ByteArrayOutputStream();
    1.64          int b;
     2.1 --- a/docker.api/src/org/netbeans/modules/docker/api/DockerAction.java	Tue Feb 02 11:16:32 2016 +0100
     2.2 +++ b/docker.api/src/org/netbeans/modules/docker/api/DockerAction.java	Tue Feb 02 11:34:56 2016 +0100
     2.3 @@ -52,7 +52,6 @@
     2.4  import org.netbeans.modules.docker.HttpUtils;
     2.5  import org.netbeans.modules.docker.DirectStreamResult;
     2.6  import org.netbeans.modules.docker.Demuxer;
     2.7 -import java.io.BufferedOutputStream;
     2.8  import java.io.BufferedReader;
     2.9  import java.io.File;
    2.10  import java.io.IOException;
    2.11 @@ -87,7 +86,6 @@
    2.12  import java.util.logging.Logger;
    2.13  import java.util.regex.Matcher;
    2.14  import java.util.regex.Pattern;
    2.15 -import javax.net.ssl.HttpsURLConnection;
    2.16  import javax.net.ssl.SSLContext;
    2.17  import javax.swing.SwingUtilities;
    2.18  import org.json.simple.JSONArray;
    2.19 @@ -100,7 +98,6 @@
    2.20  import org.netbeans.modules.docker.DockerConfig;
    2.21  import org.netbeans.modules.docker.DockerUtils;
    2.22  import org.netbeans.modules.docker.StreamResult;
    2.23 -import org.openide.filesystems.FileUtil;
    2.24  import org.openide.util.Pair;
    2.25  import org.openide.util.Parameters;
    2.26  import org.openide.util.RequestProcessor;
    2.27 @@ -461,7 +458,7 @@
    2.28          DockerContainerDetail info = DockerAction.this.getDetail(container);
    2.29          Socket s = null;
    2.30          try {
    2.31 -            URL url = createURL(instance.getUrl(), null);
    2.32 +            URL url = createURL(instance.getUrl());
    2.33              s = createSocket(url);
    2.34  
    2.35              OutputStream os = s.getOutputStream();
    2.36 @@ -520,26 +517,35 @@
    2.37  
    2.38          try {
    2.39              DockerName parsed = DockerName.parse(imageName);
    2.40 -            URL httpUrl = createURL(instance.getUrl(), "/images/create?fromImage="
    2.41 -                    + HttpUtils.encodeParameter(imageName));
    2.42 -            HttpURLConnection conn = createConnection(httpUrl);
    2.43 +            URL realUrl = createURL(instance.getUrl());
    2.44 +            Socket s = createSocket(realUrl);
    2.45              try {
    2.46 -                conn.setRequestMethod("POST");
    2.47 +                OutputStream os = s.getOutputStream();
    2.48 +                os.write(("POST /images/create?fromImage="
    2.49 +                    + HttpUtils.encodeParameter(imageName) + " HTTP/1.1\r\n").getBytes("ISO-8859-1"));
    2.50                  Pair<String, String> authHeader = null;
    2.51                  JSONObject auth = createAuthObject(CredentialsManager.getDefault().getCredentials(parsed.getRegistry()));
    2.52                  authHeader = Pair.of("X-Registry-Auth", HttpUtils.encodeBase64(auth.toJSONString()));
    2.53 -                HttpUtils.configureHeaders(conn, DockerConfig.getDefault().getHttpHeaders(),
    2.54 +                HttpUtils.configureHeaders(os, DockerConfig.getDefault().getHttpHeaders(),
    2.55                          ACCEPT_JSON_HEADER, authHeader);
    2.56 +                os.write(("\r\n").getBytes("ISO-8859-1"));
    2.57 +                os.flush();
    2.58  
    2.59 -                if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
    2.60 -                    String error = HttpUtils.readError(conn);
    2.61 -                    throw codeToException(conn.getResponseCode(),
    2.62 -                            error != null ? error : conn.getResponseMessage());
    2.63 +                InputStream is = s.getInputStream();
    2.64 +                HttpUtils.Response response = HttpUtils.readResponse(is);
    2.65 +                int responseCode = response.getCode();
    2.66 +
    2.67 +                is = HttpUtils.getResponseStream(is, response, false);
    2.68 +
    2.69 +                if (responseCode != HttpURLConnection.HTTP_OK) {
    2.70 +                    String error = HttpUtils.readContent(is, response);
    2.71 +                    throw codeToException(responseCode,
    2.72 +                            error != null ? error : response.getMessage());
    2.73                  }
    2.74  
    2.75                  String authFailure = null;
    2.76                  JSONParser parser = new JSONParser();
    2.77 -                try (InputStreamReader r = new InputStreamReader(conn.getInputStream(), HttpUtils.getCharset(conn))) {
    2.78 +                try (InputStreamReader r = new InputStreamReader(is, HttpUtils.getCharset(response))) {
    2.79                      String line;
    2.80                      while ((line = readEventObject(r)) != null) {
    2.81                          JSONObject o = (JSONObject) parser.parse(line);
    2.82 @@ -560,7 +566,7 @@
    2.83                      throw new DockerAuthenticationException(authFailure);
    2.84                  }
    2.85              } finally {
    2.86 -                conn.disconnect();
    2.87 +                closeSocket(s);
    2.88              }
    2.89          } catch (MalformedURLException e) {
    2.90              throw new DockerException(e);
    2.91 @@ -589,25 +595,34 @@
    2.92                  action.append("?tag=").append(HttpUtils.encodeParameter(tagString));
    2.93              }
    2.94  
    2.95 -            URL httpUrl = createURL(instance.getUrl(), action.toString());
    2.96 -            HttpURLConnection conn = createConnection(httpUrl);
    2.97 +            URL realUrl = createURL(instance.getUrl());
    2.98 +            Socket s = createSocket(realUrl);
    2.99              try {
   2.100 -                conn.setRequestMethod("POST");
   2.101 +                OutputStream os = s.getOutputStream();
   2.102 +                os.write(("POST " + action.toString() + " HTTP/1.1\r\n").getBytes("ISO-8859-1"));
   2.103                  Pair<String, String> authHeader = null;
   2.104                  JSONObject auth = createAuthObject(CredentialsManager.getDefault().getCredentials(parsed.getRegistry()));
   2.105                  authHeader = Pair.of("X-Registry-Auth", HttpUtils.encodeBase64(auth.toJSONString()));
   2.106 -                HttpUtils.configureHeaders(conn, DockerConfig.getDefault().getHttpHeaders(),
   2.107 +                HttpUtils.configureHeaders(os, DockerConfig.getDefault().getHttpHeaders(),
   2.108                          ACCEPT_JSON_HEADER, authHeader);
   2.109 +                os.write(("\r\n").getBytes("ISO-8859-1"));
   2.110 +                os.flush();
   2.111  
   2.112 -                if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
   2.113 -                    String error = HttpUtils.readError(conn);
   2.114 -                    throw codeToException(conn.getResponseCode(),
   2.115 -                            error != null ? error : conn.getResponseMessage());
   2.116 +                InputStream is = s.getInputStream();
   2.117 +                HttpUtils.Response response = HttpUtils.readResponse(is);
   2.118 +                int responseCode = response.getCode();
   2.119 +
   2.120 +                is = HttpUtils.getResponseStream(is, response, false);
   2.121 +
   2.122 +                if (responseCode != HttpURLConnection.HTTP_OK) {
   2.123 +                    String error = HttpUtils.readContent(is, response);
   2.124 +                    throw codeToException(responseCode,
   2.125 +                            error != null ? error : response.getMessage());
   2.126                  }
   2.127  
   2.128                  String authFailure = null;
   2.129                  JSONParser parser = new JSONParser();
   2.130 -                try (InputStreamReader r = new InputStreamReader(conn.getInputStream(), HttpUtils.getCharset(conn))) {
   2.131 +                try (InputStreamReader r = new InputStreamReader(is, HttpUtils.getCharset(response))) {
   2.132                      String line;
   2.133                      while ((line = readEventObject(r)) != null) {
   2.134                          JSONObject o = (JSONObject) parser.parse(line);
   2.135 @@ -628,7 +643,7 @@
   2.136                      throw new DockerAuthenticationException(authFailure);
   2.137                  }
   2.138              } finally {
   2.139 -                conn.disconnect();
   2.140 +                closeSocket(s);
   2.141              }
   2.142          } catch (MalformedURLException e) {
   2.143              throw new DockerException(e);
   2.144 @@ -665,7 +680,7 @@
   2.145  
   2.146                  Socket s = null;
   2.147                  try {
   2.148 -                    URL url = createURL(instance.getUrl(), null);
   2.149 +                    URL url = createURL(instance.getUrl());
   2.150                      s = createSocket(url);
   2.151                      synchronized (handler) {
   2.152                          if (handler.isCancelled()) {
   2.153 @@ -837,7 +852,7 @@
   2.154          DockerContainerDetail info = getDetail(container);
   2.155          Socket s = null;
   2.156          try {
   2.157 -            URL url = createURL(instance.getUrl(), null);
   2.158 +            URL url = createURL(instance.getUrl());
   2.159              s = createSocket(url);
   2.160  
   2.161              OutputStream os = s.getOutputStream();
   2.162 @@ -891,7 +906,7 @@
   2.163      public Pair<DockerContainer, ActionStreamResult> run(String name, JSONObject configuration) throws DockerException {
   2.164          Socket s = null;
   2.165          try {
   2.166 -            URL url = createURL(instance.getUrl(), null);
   2.167 +            URL url = createURL(instance.getUrl());
   2.168              s = createSocket(url);
   2.169  
   2.170              byte[] data = configuration.toJSONString().getBytes("UTF-8");
   2.171 @@ -962,7 +977,7 @@
   2.172      boolean ping() {
   2.173          assert !SwingUtilities.isEventDispatchThread() : "Remote access invoked from EDT";
   2.174          try {
   2.175 -            URL realUrl = createURL(instance.getUrl(), null);
   2.176 +            URL realUrl = createURL(instance.getUrl());
   2.177              Socket s = createSocket(realUrl);
   2.178              try {
   2.179                  OutputStream os = s.getOutputStream();
   2.180 @@ -989,7 +1004,7 @@
   2.181          assert !SwingUtilities.isEventDispatchThread() : "Remote access invoked from EDT";
   2.182  
   2.183          try {
   2.184 -            URL url = createURL(instance.getUrl(), null);
   2.185 +            URL url = createURL(instance.getUrl());
   2.186              Socket s = createSocket(url);
   2.187              try {
   2.188                  OutputStream os = s.getOutputStream();
   2.189 @@ -1050,7 +1065,7 @@
   2.190          assert !SwingUtilities.isEventDispatchThread() : "Remote access invoked from EDT";
   2.191  
   2.192          try {
   2.193 -            URL realUrl = createURL(instance.getUrl(), null);
   2.194 +            URL realUrl = createURL(instance.getUrl());
   2.195              Socket s = createSocket(realUrl);
   2.196              try {
   2.197                  OutputStream os = s.getOutputStream();
   2.198 @@ -1092,7 +1107,7 @@
   2.199          assert !SwingUtilities.isEventDispatchThread() : "Remote access invoked from EDT";
   2.200  
   2.201          try {
   2.202 -            URL realUrl = createURL(instance.getUrl(), null);
   2.203 +            URL realUrl = createURL(instance.getUrl());
   2.204              Socket s = createSocket(realUrl);
   2.205              try {
   2.206                  OutputStream os = s.getOutputStream();
   2.207 @@ -1138,7 +1153,7 @@
   2.208          assert !SwingUtilities.isEventDispatchThread() : "Remote access invoked from EDT";
   2.209  
   2.210          try {
   2.211 -            URL realUrl = createURL(instance.getUrl(), null);
   2.212 +            URL realUrl = createURL(instance.getUrl());
   2.213              Socket s = createSocket(realUrl);
   2.214              try {
   2.215                  OutputStream os = s.getOutputStream();
   2.216 @@ -1223,20 +1238,7 @@
   2.217          }
   2.218      }
   2.219  
   2.220 -    private HttpURLConnection createConnection(URL url) throws IOException {
   2.221 -        assert "http".equals(url.getProtocol()) || "https".equals(url.getProtocol()); // NOI18N
   2.222 -        try {
   2.223 -            HttpURLConnection ret = (HttpURLConnection) url.openConnection(ProxySelector.getDefault().select(url.toURI()).get(0));
   2.224 -            if (ret instanceof HttpsURLConnection) {
   2.225 -                ((HttpsURLConnection) ret).setSSLSocketFactory(ContextProvider.getInstance().getSSLContext(instance).getSocketFactory());
   2.226 -            }
   2.227 -            return ret;
   2.228 -        } catch (URISyntaxException ex) {
   2.229 -            throw new IOException(ex);
   2.230 -        }
   2.231 -    }
   2.232 -
   2.233 -    private static URL createURL(@NonNull String url, @NullAllowed String action) throws MalformedURLException, UnsupportedEncodingException {
   2.234 +    private static URL createURL(@NonNull String url) throws MalformedURLException {
   2.235          // FIXME optimize
   2.236          String realUrl;
   2.237          if (url.startsWith("tcp://")) {
   2.238 @@ -1247,9 +1249,6 @@
   2.239          if (realUrl.endsWith("/")) {
   2.240              realUrl = realUrl.substring(0, realUrl.length() - 1);
   2.241          }
   2.242 -        if (action != null) {
   2.243 -            realUrl += action;
   2.244 -        }
   2.245  
   2.246          return new URL(realUrl);
   2.247      }