예제 #1
0
  protected HttpResponse executeRequestWithTimeout(HttpUriRequest httpReq) throws Exception {

    // Set timeout for the socket, connection manager
    // and connection itself
    if (httpConnectionTimeout > 0) {
      HttpParams httpParams = http.getParams();
      httpParams.setIntParameter("http.socket.timeout", httpConnectionTimeout);
      httpParams.setIntParameter("http.connection-manager.timeout", httpConnectionTimeout);
      httpParams.setIntParameter("http.connection.timeout", httpConnectionTimeout);
    }
    return http.execute(httpReq, ctx);
  }
  private HttpClient newHttpClient(String sslProtocol, HttpRequest request) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, httpTimeoutsProvider.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(params, httpTimeoutsProvider.getSocketTimeout());

    // AG-1059: Need to follow redirects to ensure that if the host app is setup with Apache and
    // mod_jk we can still
    // fetch internal gadgets.
    if (request.getFollowRedirects()) {
      params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
      params.setIntParameter(ClientPNames.MAX_REDIRECTS, 3);
    }
    params.setParameter(
        ClientPNames.DEFAULT_HEADERS,
        ImmutableSet.of(new BasicHeader("Accept-Encoding", "gzip, deflate")));
    DefaultHttpClient client = new DefaultHttpClient(params);
    // AG-1044: Need to use the JVM's default proxy configuration for requests.
    ProxySelectorRoutePlanner routePlanner =
        new ProxySelectorRoutePlanner(
            client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    client.setRoutePlanner(routePlanner);

    client.addResponseInterceptor(new GzipDeflatingInterceptor());
    client
        .getConnectionManager()
        .getSchemeRegistry()
        .register(new Scheme("https", new CustomSSLSocketFactory(sslProtocol), 443));
    return client;
  }
예제 #3
0
    public ListenerThread(ApiServer requestHandler, int port) {
      try {
        _serverSocket = new ServerSocket(port);
      } catch (IOException ioex) {
        s_logger.error("error initializing api server", ioex);
        return;
      }

      _params = new BasicHttpParams();
      _params
          .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000)
          .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
          .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
          .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
          .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

      // Set up the HTTP protocol processor
      BasicHttpProcessor httpproc = new BasicHttpProcessor();
      httpproc.addInterceptor(new ResponseDate());
      httpproc.addInterceptor(new ResponseServer());
      httpproc.addInterceptor(new ResponseContent());
      httpproc.addInterceptor(new ResponseConnControl());

      // Set up request handlers
      HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
      reqistry.register("*", requestHandler);

      // Set up the HTTP service
      _httpService =
          new HttpService(
              httpproc, new NoConnectionReuseStrategy(), new DefaultHttpResponseFactory());
      _httpService.setParams(_params);
      _httpService.setHandlerResolver(reqistry);
    }
예제 #4
0
 public static void setMaxTotalConnections(HttpParams httpparams, int i) {
   if (httpparams == null) {
     throw new IllegalArgumentException("HTTP parameters must not be null.");
   } else {
     httpparams.setIntParameter("http.conn-manager.max-total", i);
     return;
   }
 }
 /**
  * Obtains a set of reasonable default parameters for a server.
  *
  * @return default parameters
  */
 protected HttpParams newDefaultParams() {
   HttpParams params = new BasicHttpParams();
   params
       .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)
       .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
       .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
       .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
       .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "LocalTestServer/1.1");
   return params;
 }
  @Before
  public void initServer() throws Exception {
    HttpParams serverParams = new SyncBasicHttpParams();
    serverParams
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "TEST-SERVER/1.1");

    this.server = new HttpServerNio(serverParams);
    this.server.setExceptionHandler(new SimpleIOReactorExceptionHandler());
  }
  @Before
  public void initClient() throws Exception {
    HttpParams clientParams = new SyncBasicHttpParams();
    clientParams
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)
        .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.USER_AGENT, "TEST-CLIENT/1.1");

    this.client = new HttpClientNio(clientParams);
    this.client.setExceptionHandler(new SimpleIOReactorExceptionHandler());
  }
  /**
   * Create ViewNode connections and queue them up for connect.
   *
   * <p>This method also defines the connection params for each connection, including the default
   * settings like timeouts and the user agent string.
   *
   * @param addrs addresses of all the nodes it should connect to.
   * @return Returns a list of the ViewNodes.
   * @throws IOException
   */
  private List<ViewNode> createConnections(List<InetSocketAddress> addrs) throws IOException {

    List<ViewNode> nodeList = new LinkedList<ViewNode>();

    for (InetSocketAddress a : addrs) {
      HttpParams params = new SyncBasicHttpParams();
      params
          .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
          .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000)
          .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
          .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
          .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
          .setParameter(CoreProtocolPNames.USER_AGENT, "Couchbase Java Client 1.0.2");

      HttpProcessor httpproc =
          new ImmutableHttpProcessor(
              new HttpRequestInterceptor[] {
                new RequestContent(), new RequestTargetHost(),
                new RequestConnControl(), new RequestUserAgent(),
                new RequestExpectContinue(),
              });

      AsyncNHttpClientHandler protocolHandler =
          new AsyncNHttpClientHandler(
              httpproc,
              new MyHttpRequestExecutionHandler(this),
              new DefaultConnectionReuseStrategy(),
              new DirectByteBufferAllocator(),
              params);
      protocolHandler.setEventListener(new EventLogger());

      AsyncConnectionManager connMgr =
          new AsyncConnectionManager(
              new HttpHost(a.getHostName(), a.getPort()),
              NUM_CONNS,
              protocolHandler,
              params,
              new RequeueOpCallback(this));
      getLogger().info("Added %s to connect queue", a.getHostName());

      ViewNode node = connFactory.createViewNode(a, connMgr);
      node.init();
      nodeList.add(node);
    }

    return nodeList;
  }
예제 #9
0
	public void start()
	{
		
		
		serverParams = new BasicHttpParams();
		int so_timeout = this.params.getInt("http.socket.timeout", 30);//以秒为单位
		int SOCKET_BUFFER_SIZE = this.params.getInt("http.socket.buffer-size",8 * 1024);
		boolean STALE_CONNECTION_CHECK = this.params.getBoolean("http.connection.stalecheck", false);
		boolean TCP_NODELAY = this.params.getBoolean("TCP_NODELAY", true);
		String ORIGIN_SERVER = this.params.getString("http.origin-server", "RPC-SERVER/1.1");
		int CONNECTION_TIMEOUT = this.params.getInt("http.connection.timeout",30); 
		int httpsoLinger = this.params.getInt("http.soLinger",-1);
		serverParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, so_timeout * 1000)
				.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
						SOCKET_BUFFER_SIZE).setBooleanParameter(
						CoreConnectionPNames.STALE_CONNECTION_CHECK, STALE_CONNECTION_CHECK)
				.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, TCP_NODELAY)
				.setParameter(CoreProtocolPNames.ORIGIN_SERVER,
						ORIGIN_SERVER).setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT * 1000).setIntParameter(CoreConnectionPNames.SO_LINGER, httpsoLinger);
		
		if(!enablessl)
		{
			try {
				this.startHttp();
				System.out.println("Http server is listenig at port " + port + ",ip is " + this.ip);
		        System.out.println("Http server started.");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			try {
				this.startHttps();
				System.out.println("Https server is listenig at port " + port + ",ip is " + this.ip);
		        System.out.println("Https server started.");
		        
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(this.started )
			ApplicationContext.addShutdownHook(new ShutDownHttpServer(this));
	}
예제 #10
0
  public HttpCoreServer(int port) throws IOException {
    super();
    if (port <= 0) {
      throw new IllegalArgumentException("Server port may not be negative or null");
    }

    HttpParams params = new SyncBasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 12 * 1024)
        .setIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 1024)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpCore-Test/1.1");

    HttpProcessor httpproc =
        new ImmutableHttpProcessor(
            new HttpResponseInterceptor[] {
              new ResponseDate(),
              new ResponseServer(),
              new ResponseContent(),
              new ResponseConnControl()
            });

    HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
    reqistry.register("/rnd", new RandomDataHandler());

    HttpService httpservice =
        new HttpService(
            httpproc,
            new DefaultConnectionReuseStrategy(),
            new DefaultHttpResponseFactory(),
            reqistry,
            params);

    this.workers = new ConcurrentLinkedQueue<HttpWorker>();
    this.listener =
        new HttpListener(
            new ServerSocket(port), httpservice, new StdHttpWorkerCallback(this.workers));
  }
예제 #11
0
 /**
  * Sets the maximum number of connections allowed.
  *
  * @param params HTTP parameters
  * @param maxTotalConnections The maximum number of connections allowed.
  */
 public static void setMaxTotalConnections(
     final HttpParams params, final int maxTotalConnections) {
   Args.notNull(params, "HTTP parameters");
   params.setIntParameter(MAX_TOTAL_CONNECTIONS, maxTotalConnections);
 }
예제 #12
0
 /**
  * Sets value of the {@link NIOReactorPNames#CONTENT_BUFFER_SIZE} parameter.
  *
  * @param params HTTP parameters.
  * @param size content buffer size.
  */
 public static void setContentBufferSize(final HttpParams params, final int size) {
   Args.notNull(params, "HTTP parameters");
   params.setIntParameter(CONTENT_BUFFER_SIZE, size);
 }
예제 #13
0
  private HttpResponse phaRequestPart1(
      String reqMeth,
      String reletivePath,
      Object queryString,
      String phaToken,
      String phaTokenSecret,
      Object requestBody, // String or byte[]
      String contentType,
      Map<String, Object> options)
      throws IndivoClientException {

    String consumerToken = null;
    String consumerSecret = null;
    String foreignURL = null;
    Object indivoInstallation = options.get("indivoInstallation");
    if (indivoInstallation != null) {
      if (!(indivoInstallation instanceof String[])) {
        throw new IndivoClientException(
            "indivoInstallation option must be of type String[] with lenght == 3.  Was: "
                + indivoInstallation.getClass().getName());
      }
      String[] indivoInstallation0 = (String[]) indivoInstallation;
      if (indivoInstallation0.length != 3) {
        throw new IndivoClientException(
            "indivoInstallation option must be a String array with length 3. Length is: "
                + indivoInstallation0.length);
      }
      foreignURL = indivoInstallation0[0];
      consumerToken = indivoInstallation0[1];
      consumerSecret = indivoInstallation0[2];
    }

    logger.info(
        "consumerToken, consumerSecret, foreignURL: "
            + consumerToken
            + ", "
            + consumerSecret
            + ", "
            + foreignURL);
    String displayQS = "null";
    if (queryString != null) {
      displayQS = queryString.getClass().getName() + " " + queryString;
    }
    ;

    logger.info(
        "reletivePath, queryString, requestXmlOrParams: "
            + reletivePath
            + ",  "
            + displayQS
            + '\n'
            + requestBody
            + "\n\n");
    String queryString0;
    if (queryString == null
        || ((queryString instanceof String) && ((String) queryString).length() == 0)) {
      queryString0 = "";
    } else if (queryString instanceof String) {
      String qsString = (String) queryString;
      if (qsString.indexOf('=') < 1) {
        throw new IndivoClientException(
            "unexpected queryString, did not have any key/value delimiter of '=': " + queryString);
      }
      queryString0 = qsString;
      logger.info("queryString0 = qsString = " + qsString);
    } else if (queryString instanceof Map) {
      StringBuffer qsBuff = new StringBuffer();
      Map qsMap = (Map) queryString;
      Iterator iter = qsMap.keySet().iterator();
      while (iter.hasNext()) {
        if (qsBuff.length() > 0) {
          qsBuff.append('&');
        }

        Object keyObj = iter.next();
        if (!(keyObj instanceof String)) {
          throw new IndivoClientException(
              "queryString map key of unexpected type: "
                  + keyObj.getClass().getName()
                  + " -- "
                  + keyObj);
        }
        String key = (String) keyObj;

        Object valueObj = qsMap.get(key);
        try {
          if (valueObj instanceof String) {
            qsBuff.append(
                URLEncoder.encode(key, "UTF-8")
                    + '='
                    + URLEncoder.encode((String) valueObj, "UTF-8"));
          } else if (valueObj instanceof String[]) {
            String[] valueArr = (String[]) valueObj;
            for (int ii = 0; ii < valueArr.length; ii++) {
              qsBuff.append(
                  URLEncoder.encode(key, "UTF-8") + '=' + URLEncoder.encode(valueArr[ii], "UTF-8"));
            }
          } else {
            throw new IndivoClientException(
                "queryString map value of unexpected type: "
                    + valueObj.getClass().getName()
                    + " -- "
                    + valueObj);
          }
        } catch (java.io.UnsupportedEncodingException uee) {
          throw new IndivoClientException(uee);
        }
      }
      queryString0 = qsBuff.toString();
    } else {
      throw new IndivoClientException(
          "queryString not String or Map, type is: " + queryString.getClass().getName());
    }

    String baseURL0 = defaultBaseURL;
    if (foreignURL != null) {
      baseURL0 = foreignURL;
    }

    String consumerKey0 = defaultConsumerKey;
    String consumerSecret0 = defaultConsumerSecret;
    if (consumerToken != null) {
      consumerKey0 = consumerToken;
      consumerSecret0 = consumerSecret;
    }

    logger.info(
        " -- baseURL0: "
            + baseURL0
            + " -- reletivePath: "
            + reletivePath
            + " -- queryString: "
            + queryString0);

    String phaURLString = baseURL0 + reletivePath;
    if (queryString0.length() > 0) {
      phaURLString += "?" + queryString0;
    }

    /* FIXME temp for test*/
    // System.out.println(phaURLString); if (requestBody != null) { System.out.println(requestBody);
    // }

    if (requestBody != null) {
      if (requestBody instanceof String) {
        if (((String) requestBody).length() > 0) {
          if (contentType == null || contentType.length() == 0) {
            throw new IndivoClientException("contentType must be provided for request body");
          }
        }
      } else if ((requestBody instanceof Byte[] && ((Byte[]) requestBody).length > 0)
          || (requestBody instanceof byte[] && ((byte[]) requestBody).length > 0)) {
        if (contentType == null || contentType.length() == 0) {
          throw new IndivoClientException("contentType must be provided for request body");
        }
      } else {
        throw new IndivoClientException(
            "requestBody must be either String or Byte[] or byte[], was: "
                + requestBody.getClass().getName());
      }
    } else if (contentType != null && contentType.length() > 0) {
      throw new IndivoClientException("content type provided without requestBody: " + contentType);
    }

    HttpUriRequest hcRequest = null;

    // String requestXmlOrParams0 = null;
    if (requestBody == null) {
      requestBody = "";
    }

    logger.info("reqMeth: " + reqMeth);
    try {
      if (reqMeth.equals("PUT") || reqMeth.equals("POST")) {
        if (reqMeth.equals("PUT")) {
          hcRequest = new HttpPut(phaURLString);
        } else {
          hcRequest = new HttpPost(phaURLString);
        }

        byte[] requestBodyB = null;
        if (requestBody instanceof String) {
          String requestBodyStr = (String) requestBody;
          if (requestBodyStr.startsWith("<?xml")) {
            String[] parsedProlog = getEncoding(requestBodyStr);
            if (parsedProlog.length == 3 && (!parsedProlog[1].toUpperCase().equals("UTF-8"))) {
              requestBodyStr = parsedProlog[0] + "UTF-8" + parsedProlog[1];
              logger.info("changing prolog from: " + parsedProlog[1] + " to: " + "UTF-8");
              requestBodyStr = parsedProlog[0] + "UTF-8" + parsedProlog[2];
            }
          }

          // System.out.println("requestBodyStr: " + requestBodyStr);
          requestBodyB = requestBodyStr.getBytes("UTF-8");
        } else if (requestBody instanceof byte[]) {
          requestBodyB = (byte[]) requestBody;
        } else { // requestBody instanceof Byte[]
          requestBodyB = new byte[((Byte[]) requestBody).length];
          for (int ii = 0; ii < ((Byte[]) requestBody).length; ii++) {
            requestBodyB[ii] = ((Byte[]) requestBody)[ii];
          }
        }
        ByteArrayEntity bae = new ByteArrayEntity(requestBodyB);
        bae.setContentType(contentType);
        ((HttpEntityEnclosingRequestBase) hcRequest).setEntity(bae);
        //                hcRequest.addHeader("Content-Type",contentType);
      } else if (reqMeth.equals("GET")) {
        hcRequest = new HttpGet(phaURLString);
      } else if (reqMeth.equals("DELETE")) {
        hcRequest = new HttpDelete(phaURLString);
      }
    } catch (java.io.UnsupportedEncodingException uee) {
      throw new IndivoClientException(uee);
    }

    // in case of form-url-encoded, will signpost know to look at Content-Type header and entity??

    logger.info("pre signWithSignpost");
    signWithSignpost(hcRequest, consumerKey0, consumerSecret0, phaToken, phaTokenSecret);
    logger.info("post signWithSignpost");

    hcRequest.addHeader("Accept", "text/plain,application/xml"); // don't be mistaken for a browser
    logger.info("post signWithSignpost 1");

    AbstractHttpClient httpClient = new DefaultHttpClient();
    HttpParams httpParams0 = httpClient.getParams();
    logger.info("post signWithSignpost 2");

    Object connectionTimeout = options.get("connectionTimeout");
    Object socketTimeout = options.get("socketTimeout");
    logger.info("post signWithSignpost 3");
    if (connectionTimeout == null) {
      connectionTimeout = defaultHttpTimeout;
    }
    if (socketTimeout == null) {
      socketTimeout = defaultHttpTimeout;
    }
    logger.info("post signWithSignpost 4");

    if (!((socketTimeout instanceof Integer) && (connectionTimeout instanceof Integer))) {
      throw new IndivoClientException(
          "socketTimeout and connectionTimeout options must be ingeters. "
              + "sockenTimeout was "
              + socketTimeout.getClass().getName()
              + ", and connectionTimeout was "
              + connectionTimeout.getClass().getName());
    }
    logger.info("about to set CONNECTION_TIMEOUT");

    httpParams0 =
        httpParams0.setIntParameter(
            CoreConnectionPNames.CONNECTION_TIMEOUT, (Integer) connectionTimeout);
    httpParams0 =
        httpParams0.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, (Integer) socketTimeout);
    httpClient.setParams(httpParams0);

    HttpResponse httpResponse = null;
    //        StatusLine statusLine = null;
    //        InputStream istrm = null;
    org.apache.http.Header[] allheaders = hcRequest.getAllHeaders();
    StringBuffer allheadersSB = new StringBuffer("\nall request headers:");
    for (int ii = 0; ii < allheaders.length; ii++) {
      allheadersSB.append("\n" + allheaders[ii].getName() + " : " + allheaders[ii].getValue());
    }
    logger.info("request: " + hcRequest.getMethod() + " " + hcRequest.getURI() + allheadersSB);
    try {
      httpResponse = httpClient.execute(hcRequest);
    } catch (java.net.ConnectException conE) {
      conE.printStackTrace();
      logger.warn("connectionTimeout, socketTimeout: " + connectionTimeout + ", " + socketTimeout);
      throw new IndivoClientConnectException(
          "connectionTimeout, socketTimeout: " + connectionTimeout + ", " + socketTimeout, conE);
    } catch (Exception excp) {
      excp.printStackTrace();
      logger.warn("phaRequestPart1 exception");
      throw new IndivoClientException(
          "connectionTimeout, socketTimeout: " + connectionTimeout + ", " + socketTimeout, excp);
    }

    return httpResponse;
  }
  /**
   * Initialize the transport sender, and execute reactor in new separate thread
   *
   * @param cfgCtx the Axis2 configuration context
   * @param transportOut the description of the http/s transport from Axis2 configuration
   * @throws AxisFault thrown on an error
   */
  public void init(ConfigurationContext cfgCtx, TransportOutDescription transportOut)
      throws AxisFault {
    this.configurationContext = cfgCtx;

    cfg = NHttpConfiguration.getInstance();
    params = new BasicHttpParams();
    params
        .setIntParameter(
            CoreConnectionPNames.SO_TIMEOUT,
            cfg.getProperty(NhttpConstants.SO_TIMEOUT_SENDER, 60000))
        .setIntParameter(
            CoreConnectionPNames.CONNECTION_TIMEOUT,
            cfg.getProperty(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000))
        .setIntParameter(
            CoreConnectionPNames.SOCKET_BUFFER_SIZE,
            cfg.getProperty(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024))
        .setParameter(CoreProtocolPNames.USER_AGENT, "Synapse-HttpComponents-NIO");
    //                .setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET,
    //
    // cfg.getStringValue(CoreProtocolPNames.HTTP_ELEMENT_CHARSET,HTTP.DEFAULT_PROTOCOL_CHARSET));
    // //TODO:This does not works with HTTPCore 4.3

    name = transportOut.getName().toUpperCase(Locale.US) + " Sender";

    ClientConnFactoryBuilder contextBuilder = initConnFactoryBuilder(transportOut);
    connFactory = contextBuilder.createConnFactory(params);

    connpool = new ConnectionPool();

    proxyConfig = new ProxyConfigBuilder().build(transportOut);
    log.info(proxyConfig.logProxyConfig());

    Parameter param = transportOut.getParameter("warnOnHTTP500");
    if (param != null) {
      String[] warnOnHttp500 = ((String) param.getValue()).split("\\|");
      cfgCtx.setNonReplicableProperty("warnOnHTTP500", warnOnHttp500);
    }

    IOReactorConfig ioReactorConfig = new IOReactorConfig();
    ioReactorConfig.setIoThreadCount(NHttpConfiguration.getInstance().getClientIOWorkers());
    ioReactorConfig.setSoTimeout(cfg.getProperty(NhttpConstants.SO_TIMEOUT_RECEIVER, 60000));
    ioReactorConfig.setConnectTimeout(
        cfg.getProperty(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000));
    ioReactorConfig.setTcpNoDelay(cfg.getProperty(CoreConnectionPNames.TCP_NODELAY, 1) == 1);
    if (cfg.getBooleanValue("http.nio.interest-ops-queueing", false)) {
      ioReactorConfig.setInterestOpQueued(true);
    }

    try {
      String prefix = name + " I/O dispatcher";
      ioReactor =
          new DefaultConnectingIOReactor(
              ioReactorConfig,
              new NativeThreadFactory(new ThreadGroup(prefix + " thread group"), prefix));
      ioReactor.setExceptionHandler(
          new IOReactorExceptionHandler() {
            public boolean handle(IOException ioException) {
              log.warn(
                  "System may be unstable: IOReactor encountered a checked exception : "
                      + ioException.getMessage(),
                  ioException);
              return true;
            }

            public boolean handle(RuntimeException runtimeException) {
              log.warn(
                  "System may be unstable: IOReactor encountered a runtime exception : "
                      + runtimeException.getMessage(),
                  runtimeException);
              return true;
            }
          });
    } catch (IOException e) {
      log.error("Error starting the IOReactor", e);
      throw new AxisFault(e.getMessage(), e);
    }

    metrics = new NhttpMetricsCollector(false, transportOut.getName());
    handler = new ClientHandler(connpool, connFactory, proxyConfig, cfgCtx, params, metrics);
    iodispatch = new ClientIODispatch(handler, connFactory);
    final IOEventDispatch ioEventDispatch = iodispatch;

    // start the Sender in a new seperate thread
    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                try {
                  ioReactor.execute(ioEventDispatch);
                } catch (InterruptedIOException ex) {
                  log.fatal("Reactor Interrupted");
                } catch (IOException e) {
                  log.fatal("Encountered an I/O error: " + e.getMessage(), e);
                }
                log.info(name + " Shutdown");
              }
            },
            "HttpCoreNIOSender");
    t.start();
    log.info(name + " starting");

    // register with JMX
    mbeanSupport = new TransportMBeanSupport(this, "nio-" + transportOut.getName());
    mbeanSupport.register();

    state = BaseConstants.STARTED;
  }
예제 #15
0
  public void run() {
    logger.info("Starting server.");

    int localPort = MaryProperties.needInteger("socket.port");

    HttpParams params = new BasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0)
        // 0 means no timeout, any positive value means time out in miliseconds (i.e. 50000 for 50
        // seconds)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new ResponseDate());
    httpproc.addInterceptor(new ResponseServer());
    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseConnControl());

    BufferingHttpServiceHandler handler =
        new BufferingHttpServiceHandler(
            httpproc,
            new DefaultHttpResponseFactory(),
            new DefaultConnectionReuseStrategy(),
            params);

    // Set up request handlers
    HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
    registry.register("/process", new SynthesisRequestHandler());
    InfoRequestHandler infoRH = new InfoRequestHandler();
    registry.register("/version", infoRH);
    registry.register("/datatypes", infoRH);
    registry.register("/locales", infoRH);
    registry.register("/voices", infoRH);
    registry.register("/audioformats", infoRH);
    registry.register("/exampletext", infoRH);
    registry.register("/audioeffects", infoRH);
    registry.register("/audioeffect-default-param", infoRH);
    registry.register("/audioeffect-full", infoRH);
    registry.register("/audioeffect-help", infoRH);
    registry.register("/audioeffect-is-hmm-effect", infoRH);
    registry.register("/features", infoRH);
    registry.register("/features-discrete", infoRH);
    registry.register("/vocalizations", infoRH);
    registry.register("/styles", infoRH);
    registry.register("*", new FileRequestHandler());

    handler.setHandlerResolver(registry);

    // Provide an event logger
    handler.setEventListener(new EventLogger());

    IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, params);

    int numParallelThreads = MaryProperties.getInteger("server.http.parallelthreads", 5);

    logger.info("Waiting for client to connect on port " + localPort);

    try {
      ListeningIOReactor ioReactor = new DefaultListeningIOReactor(numParallelThreads, params);
      ioReactor.listen(new InetSocketAddress(localPort));
      isReady = true;
      ioReactor.execute(ioEventDispatch);
    } catch (InterruptedIOException ex) {
      logger.info("Interrupted", ex);
    } catch (IOException e) {
      logger.info("Problem with HTTP connection", e);
    }
    logger.debug("Shutdown");
  }
예제 #16
0
  /**
   * Creates a handler with the specified input URL, max thread count, and message backlog support.
   *
   * @param inputUrl - the URL provided by Loggly for sending log messages to
   * @param maxThreads - the max number of concurrent background threads that are allowed to send
   *     data to Loggly
   * @param backlog - the max number of log messages that can be queued up (anything beyond will be
   *     thrown away)
   */
  public LogglyHandler(String inputUrl, int maxThreads, int backlog) {
    this.inputUrl = inputUrl;

    pool =
        new ThreadPoolExecutor(
            maxThreads,
            maxThreads,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>(backlog),
            new ThreadFactory() {
              @Override
              public Thread newThread(Runnable r) {
                Thread thread = new Thread(r, "Loggly Thread");
                thread.setDaemon(true);
                return thread;
              }
            },
            new ThreadPoolExecutor.DiscardOldestPolicy());
    pool.allowCoreThreadTimeOut(true);

    retryQueue = new LinkedBlockingQueue<LogglySample>(backlog);

    Thread retryThread =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                while (allowRetry) {
                  // drain the retry requests
                  LogglySample sample = null;
                  while ((sample = retryQueue.poll()) != null) {
                    if (sample.retryCount > 10) {
                      // todo: capture statistics about the failure (exception and/or status code)
                      //       and then report on it in some sort of thoughtful way to standard err
                    } else {
                      pool.submit(sample);
                    }
                  }

                  // retry every 10 seconds
                  try {
                    Thread.sleep(10000);
                  } catch (InterruptedException e) {
                    System.err.println("Retry sleep was interrupted, giving up on retry thread");
                    return;
                  }
                }
              }
            },
            "Loggly Retry Thread");
    retryThread.setDaemon(true);
    retryThread.start();

    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, maxThreads);
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(maxThreads);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

    // set 15 second timeouts, since Loggly should return quickly
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);

    SchemeRegistry registry = new SchemeRegistry();
    try {
      registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    } catch (Exception e) {
      throw new RuntimeException("Could not register SSL socket factor for Loggly", e);
    }

    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, registry);

    httpClient = new DefaultHttpClient(connManager, params);

    // because the threads a daemon threads, we want to give them a chance
    // to finish up before we totally shut down
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    close();
                  }
                }));
  }
  public static void main(String[] args) throws Exception {
    HttpParams params = new BasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
        .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    // Set up protocol handler
    BufferingHttpClientHandler protocolHandler =
        new BufferingHttpClientHandler(
            httpproc,
            new MyHttpRequestExecutionHandler(),
            new DefaultConnectionReuseStrategy(),
            params);
    protocolHandler.setEventListener(new EventLogger());

    // Limit the total maximum of concurrent connections to 5
    int maxTotalConnections = 5;

    // Use the connection manager to maintain a pool of connections to localhost:8080
    final AsyncConnectionManager connMgr =
        new AsyncConnectionManager(
            new HttpHost("localhost", 8080), maxTotalConnections, protocolHandler, params);

    // Start the I/O reactor in a separate thread
    Thread t =
        new Thread(
            new Runnable() {

              public void run() {
                try {
                  connMgr.execute(); // The connection manager wraps the I/O reactor.
                } catch (InterruptedIOException ex) {
                  System.err.println("Interrupted");
                } catch (IOException e) {
                  System.err.println("I/O error: " + e.getMessage());
                }
                System.out.println("I/O reactor terminated");
              }
            });
    t.start();

    // Submit 50 requests using maximum 5 concurrent connections
    Queue<RequestHandle> queue = new LinkedList<RequestHandle>();
    for (int i = 0; i < 50; i++) {
      AsyncConnectionRequest connRequest =
          connMgr.requestConnection(); // Get a working connection from the connection manager.
      connRequest
          .waitFor(); // Wait for the connection request to be completed to the target authority.
      NHttpClientConnection conn =
          connRequest.getConnection(); // Get the real connection from the connection request.
      if (conn == null) {
        System.err.println("Failed to obtain connection");
        break;
      }

      HttpContext context = conn.getContext();
      BasicHttpRequest httpget = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
      RequestHandle handle = new RequestHandle(connMgr, conn);

      context.setAttribute("request", httpget);
      context.setAttribute("request-handle", handle);

      queue.add(handle);
      conn.requestOutput();
    }

    // Wait until all requests have been completed
    while (!queue.isEmpty()) {
      RequestHandle handle = queue.remove();
      handle.waitFor();
    }

    // Give the I/O reactor 10 sec to shut down
    connMgr.shutdown(10000);
    System.out.println("Done");
  }
예제 #18
0
  /**
   * 构造器,进行client的参数设置,包括Header、Cookie等
   *
   * @param aconfig
   * @param cookies
   */
  public void init(SpiderConfig config, Site _site) {
    this.config = config;
    // 设置HTTP参数
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages())
      schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    httpClient.getParams().setIntParameter("http.socket.timeout", 60000);
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
    httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, config.isFollowRedirects());
    //		HttpClientParams.setCookiePolicy(httpClient.getParams(),CookiePolicy.BEST_MATCH);

    // 设置响应拦截器
    httpClient.addResponseInterceptor(
        new HttpResponseInterceptor() {
          public void process(final HttpResponse response, final HttpContext context)
              throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
              HeaderElement[] codecs = contentEncoding.getElements();
              for (HeaderElement codec : codecs) {
                // 处理GZIP解压缩
                if (codec.getName().equalsIgnoreCase("gzip")) {
                  response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                  return;
                }
              }
            }
          }
        });

    if (_site != null) {
      this.site = _site;
      if (this.site.getHeaders() != null && this.site.getHeaders().getHeader() != null) {
        for (org.eweb4j.spiderman.xml.Header header : this.site.getHeaders().getHeader()) {
          this.addHeader(header.getName(), header.getValue());
        }
      }
      if (this.site.getCookies() != null && this.site.getCookies().getCookie() != null) {
        for (org.eweb4j.spiderman.xml.Cookie cookie : this.site.getCookies().getCookie()) {
          this.addCookie(cookie.getName(), cookie.getValue(), cookie.getHost(), cookie.getPath());
        }
      }
    }
  }
예제 #19
0
  public static Client createApacheClient(
      AtmosConfig config,
      boolean useExpect100Continue,
      List<Class<MessageBodyReader<?>>> readers,
      List<Class<MessageBodyWriter<?>>> writers) {
    try {
      ClientConfig clientConfig = new DefaultApacheHttpClient4Config();

      // make sure the apache client is thread-safe
      PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
      // Increase max total connection to 200
      connectionManager.setMaxTotal(200);
      // Increase default max connection per route to 200
      connectionManager.setDefaultMaxPerRoute(200);
      clientConfig
          .getProperties()
          .put(DefaultApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER, connectionManager);

      // register an open trust manager to allow SSL connections to servers with self-signed
      // certificates
      if (config.isDisableSslValidation()) {
        connectionManager
            .getSchemeRegistry()
            .register(
                new Scheme(
                    "https",
                    443,
                    new SSLSocketFactory(
                        SslUtil.createGullibleSslContext(),
                        SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)));
      }

      // set proxy uri
      ProxySelector proxySelector;
      // first look in config
      URI proxyUri = config.getProxyUri();
      if (proxyUri != null) {
        proxySelector = new ConfigProxySelector(config);
      } else {
        // if no proxy in config, use system property sniffing selector
        proxySelector = new DefaultProxySelector();

        // and see if a proxy is set
        String host = System.getProperty("http.proxyHost");
        String portStr = System.getProperty("http.proxyPort");
        int port = (portStr != null) ? Integer.parseInt(portStr) : -1;
        if (host != null && host.length() > 0)
          proxyUri = new URI("http", null, host, port, null, null, null);
      }

      // make sure any proxy credentials (below) are associated with the proxy
      if (proxyUri != null)
        clientConfig.getProperties().put(ApacheHttpClient4Config.PROPERTY_PROXY_URI, proxyUri);

      // set proxy auth
      // first config
      String proxyUser = config.getProxyUser(), proxyPassword = config.getProxyPassword();
      // then system props
      if (proxyUser == null) {
        proxyUser = System.getProperty("http.proxyUser");
        proxyPassword = System.getProperty("http.proxyPassword");
      }
      if (proxyUser != null && proxyUser.length() > 0) {
        clientConfig
            .getProperties()
            .put(ApacheHttpClient4Config.PROPERTY_PROXY_USERNAME, proxyUser);
        clientConfig
            .getProperties()
            .put(ApacheHttpClient4Config.PROPERTY_PROXY_PASSWORD, proxyPassword);
      }

      // specify whether to use Expect: 100-continue
      HttpParams httpParams = new SyncBasicHttpParams();
      DefaultHttpClient.setDefaultHttpParams(httpParams);
      httpParams.setBooleanParameter(AllClientPNames.USE_EXPECT_CONTINUE, useExpect100Continue);
      clientConfig
          .getProperties()
          .put(DefaultApacheHttpClient4Config.PROPERTY_HTTP_PARAMS, httpParams);

      // pick up other configuration from system props
      for (String prop : System.getProperties().stringPropertyNames()) {
        if (prop.startsWith("http.")) {
          // because AbstractHttpParams uses casts instead of parsing string values, we must know
          // the
          // parameter types ahead of time. I'm going to guess them in lieu of maintaining a map.
          // however,
          // we still need to define Long typed parameters so they may be differentiated from
          // Integer typed
          // parameters.
          String value = System.getProperty(prop);
          if (LONG_PARAMETERS.contains(prop))
            httpParams.setLongParameter(
                prop, Long.parseLong(value.substring(0, value.length() - 1)));
          else if (BOOLEAN_PATTERN.matcher(value).matches())
            httpParams.setBooleanParameter(prop, Boolean.parseBoolean(value));
          else if (DOUBLE_PATTERN.matcher(value).matches())
            httpParams.setDoubleParameter(prop, Double.parseDouble(value));
          else if (INT_PATTERN.matcher(value).matches())
            httpParams.setIntParameter(prop, Integer.parseInt(value));
          else httpParams.setParameter(prop, System.getProperty(prop));
        }
      }

      JerseyUtil.addHandlers(clientConfig, readers, writers);

      // create the client
      ApacheHttpClient4 client = ApacheHttpClient4.create(clientConfig);
      AbstractHttpClient httpClient =
          (AbstractHttpClient) client.getClientHandler().getHttpClient();

      // do not use Apache's retry handler
      httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

      // use a RoutePlanner/ProxySelector that fits our requirements
      SchemeRegistry registry = httpClient.getConnectionManager().getSchemeRegistry();
      httpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector));

      JerseyUtil.addFilters(client, config);

      return client;
    } catch (Exception e) {
      throw new AtmosException("Error configuring REST client", e);
    }
  }