コード例 #1
0
 protected byte[] sendHttpMessage(Hashtable headers, byte[] body) throws IOException {
   HttpMessageContext context = HttpMessageContext.getInstance();
   setVerb(getRequestMethod());
   context.Debug("GNHttpConnection [sendHttpMessage] Method[" + getRequestMethod() + "]");
   addRequestHeaders(headers);
   if (context.getLogheader() && headers != null) {
     GTConfigFile head = new GTConfigFile(headers);
     context.Debug("GNHttpConnection [sendHttpMessage] requestHeader\r\n" + head);
   }
   return sendHttpMessage(body);
 }
コード例 #2
0
  /**
   * Get the KeyManagers for the the specified keystore
   *
   * @param ksFile The keystore to manager
   * @param ksPass The keystore password
   * @return KeyManagers that can manage the keystore
   * @throws Exception
   */
  protected KeyManager[] getKeyManagers(String ksFile, String ksPass) throws Exception {
    if (HttpMessageContext.emptyString(ksFile)) {
      ksFile = System.getProperty("javax.net.ssl.keyStore");
    }
    if (HttpMessageContext.emptyString(ksPass)) {
      ksPass = System.getProperty("javax.net.ssl.keyStorePassword", "changeit");
    }
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(new FileInputStream(ksFile), ksPass.toCharArray());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    kmf.init(ks, ksPass.toCharArray());

    return kmf.getKeyManagers();
  }
コード例 #3
0
 public byte[] doGetPOST(Hashtable headers, byte[] content) throws Exception {
   HttpMessageContext context = HttpMessageContext.getInstance();
   try {
     connect();
     sendHttpMessage(headers, content);
     return getResponseMessage();
   } catch (Exception ex) {
     context.Debug(
         "GNHttpConnection[doGetPOST] Connecton fail for "
             + url
             + "\r\n"
             + context.Exception2String(ex));
     throw ex;
   }
 }
コード例 #4
0
 private byte[] sendHttpMessage(byte[] body) throws IOException {
   HttpMessageContext context = HttpMessageContext.getInstance();
   context.Debug("GNHttpConnection [sendHttpMessage] starts");
   if (context.getLogheader()) {
     context.Info(logMessageSetting());
   }
   connection.setDoInput(true);
   if (body != null) {
     connection.setDoOutput(true);
     OutputStream os = TimedURLConnection.getOutputStream(connection, timeout * 1000);
     context.Debug("GNHttpConnection [sendHttpMessage] sending message ...");
     os.write(body);
     context.Debug("GNHttpConnection [sendHttpMessage] message sent");
   }
   context.Debug(
       "GNHttpConnection [sendHttpMessage] TimedURLConnection.getInputStream timeout["
           + timeout
           + " S]");
   InputStream is = TimedURLConnection.getInputStream(connection, timeout * 1000);
   responseCode = connection.getResponseCode();
   context.Debug("GNHttpConnection [sendHttpMessage] responseCode[" + responseCode + "]");
   responseMessage = HttpMessageContext.getMessage(is);
   responseheaders = new Hashtable<String, String>();
   int no = 0;
   while (true) {
     String headerName = connection.getHeaderFieldKey(no);
     String headerValue = connection.getHeaderField(no);
     if (headerName != null && headerName.length() > 0) {
       responseheaders.put(headerName, headerValue);
     } else {
       if (headerValue == null || headerValue.length() <= 0) break;
     }
     no++;
   }
   if (context.getLogheader()) {
     GTConfigFile head = new GTConfigFile(responseheaders);
     context.Debug("GNHttpConnection [sendHttpMessage] responseHeader\r\n" + head);
     context.Debug(
         "GNHttpConnection [sendHttpMessage] responseMessage\r\n"
             + new String(getResponseMessage()));
     context.Info("GNHttpConnection [sendHttpMessage] success for " + url);
   } else context.Info("GNHttpConnection [sendHttpMessage] success for " + url);
   return responseMessage;
 }
コード例 #5
0
  protected HttpURLConnection connect(String url) throws Exception {
    HttpMessageContext context = HttpMessageContext.getInstance();
    context.Debug("GNHttpConnection [connect] connect");
    URLConnection uc = null;
    /*
    synchronized(context)
    {
      Properties prop = System.getProperties();*/
    /*NSL20070112
    if(isUsingProxy())
    {
      prop.setProperty("proxySet", "true" );
      prop.setProperty("proxyHost", getConfig_http_proxy_url());
      prop.setProperty("proxyPort", getConfig_http_proxy_port());

      prop.setProperty("http.proxySet","true");
      prop.setProperty("http.proxyHost",getConfig_http_proxy_url());
      prop.setProperty("http.proxyPort",getConfig_http_proxy_port());
      prop.setProperty("https.proxyHost", getConfig_http_proxy_url());
      prop.setProperty("https.proxyPort", getConfig_http_proxy_port());
    }
    else
    {
      prop.remove("proxySet");
      prop.remove("proxyHost");
      prop.remove("proxyPort");

      prop.remove("http.proxySet");
      prop.remove("http.proxyHost");
      prop.remove("http.proxyPort");
      prop.remove("https.proxyHost");
      prop.remove("https.proxyPort");
    }
    Authenticator.setDefault(new AuthImpl());
    */
    /*NSL20070507 No need to change the JVM property
    if(!HttpMessageContext.emptyString(keyStoreFile))
    {
      prop.put("javax.net.ssl.keyStore", keyStoreFile);
      prop.put("javax.net.ssl.keyStorePassword", JavaKeyStoreHandler.getTrustStorePassword(keyStorePassword));
    }
    if (!HttpMessageContext.emptyString(trustStoreFile))
    {
        prop.put("javax.net.ssl.trustStore", trustStoreFile);
        prop.put("javax.net.ssl.trustStorePassword", trustStorePassword);
    }*/
    /*NSL20070112
    context.Debug("GNHttpConnection [connect]" + "Connecting URL(" + url + ") timeout[" + timeout +" s]");
    myURLobject = new URL(url);
    uc = myURLobject.openConnection();
    */
    /*}*/

    context.Debug(
        "GNHttpConnection [connect]" + "Connecting URL(" + url + ") timeout[" + timeout + " s]");
    try {
      myURLobject = new URL(url);
      List<Proxy> proxyList = findProxy(myURLobject.toURI());
      if (proxyList != null && !proxyList.isEmpty()) {
        for (Proxy proxy : proxyList) {
          try {
            uc = myURLobject.openConnection(proxy);
            break;
          } catch (Exception ex) {
          }
        }
      }
      if (uc == null) {
        uc = myURLobject.openConnection();
      }
    } catch (MalformedURLException ex) {
      context.Warn("GNHttpConnection [connect] Malformed URL: " + url, ex);
    } catch (URISyntaxException ex) {
      context.Warn("GNHttpConnection [connect] Invalid URI: " + myURLobject, ex);
    }

    connection = (HttpURLConnection) uc;
    context.Debug("GNHttpConnection [connect] Connection created");
    if (connection instanceof HttpsURLConnection) {
      context.Debug("GNHttpConnection [connect] Https Connection");
      HttpsURLConnection https = (HttpsURLConnection) connection;
      // SSLSocketFactory factory = null;
      SSLSocketFactory factory = getSSLSocketFactory(); // NSL20070507
      if (!authenticateServer) {
        // factory = new NoAuthSSLSocketFactory( https.getSSLSocketFactory() );
        factory = new NoAuthSSLSocketFactory(factory); // NSL20070507
      }

      if (factory != null) {
        https.setSSLSocketFactory(factory);
      }

      if (!verifyServerHostname) {
        https.setHostnameVerifier(new NoVerificationHostnameVerifier());
      }
    }
    return connection;
  }