Esempio n. 1
0
  private void writeHead() {
    request.setChunked(chunked);
    if (!raw) {
      request.setHeader(HttpHeaders.Names.HOST, conn.hostHeader);

      if (chunked) {
        request.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
      } else if (contentLength == 0) {
        // request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "0");
      }
    }
    conn.write(request);
  }
 /**
  * Creates an empty HttpRequest object with pre-filled standard headers
  *
  * @param uriString the request URL
  * @return the request object
  */
 protected HttpRequest createEmptyRequest(String uriString) {
   HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriString);
   request.setHeader(HttpHeaders.Names.HOST, _server.getAddress().toString());
   request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
   request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
   String hostHdr = DbusHttpUtils.getLocalHostName();
   String svcHdr = DbusConstants.getServiceIdentifier();
   if (!hostHdr.isEmpty()) {
     request.setHeader(DatabusHttpHeaders.DBUS_CLIENT_HOST_HDR, hostHdr);
   }
   if (!svcHdr.isEmpty()) {
     request.setHeader(DatabusHttpHeaders.DBUS_CLIENT_SERVICE_HDR, svcHdr);
   }
   return request;
 }
Esempio n. 3
0
 /**
  * Same as {@link #end()} but writes some data to the request body before ending. If the request
  * is not chunked and no other data has been written then the Content-Length header will be
  * automatically set
  */
 public void end(Buffer chunk) {
   if (!chunked && contentLength == 0) {
     contentLength = chunk.length();
     request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(contentLength));
   }
   write(chunk);
   end();
 }
Esempio n. 4
0
 /**
  * Inserts all the specified headers into the request. The {@link Object#toString()} method will
  * be called on the header values {@code value} to determine the String value to actually use for
  * the header value.
  *
  * <p>
  *
  * @return A reference to this, so multiple method calls can be chained.
  */
 public HttpClientRequest putAllHeaders(Map<String, ? extends Object> m) {
   check();
   for (Map.Entry<String, ? extends Object> entry : m.entrySet()) {
     request.setHeader(entry.getKey(), entry.getValue().toString());
     checkContentLengthChunked(entry.getKey(), entry.getValue());
   }
   return this;
 }
Esempio n. 5
0
  public static HttpRequest create(HttpMethod method, String action) throws URISyntaxException {
    URI uri = new URI(action);
    HttpRequest request =
        new DefaultHttpRequest(new HttpVersion("STREST", 0, 1, true), method, uri.toASCIIString());

    //        request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.setHeader("Strest-Txn-Id", generateTxnId());
    request.setProtocolVersion(new HttpVersion("STREST", 0, 1, true));
    return request;
  }
Esempio n. 6
0
  public File get() throws IOException {
    ClientBootstrap bootstrap =
        new ClientBootstrap(
            new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    bootstrap.setOption("connectTimeoutMillis", 5000L); // set 5 sec
    bootstrap.setOption("receiveBufferSize", 1048576); // set 1M
    ChannelPipelineFactory factory = new HttpClientPipelineFactory(file);
    bootstrap.setPipelineFactory(factory);

    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

    // Wait until the connection attempt succeeds or fails.
    Channel channel = future.awaitUninterruptibly().getChannel();
    if (!future.isSuccess()) {
      bootstrap.releaseExternalResources();
      throw new IOException(future.getCause());
    }

    String query = uri.getPath() + (uri.getQuery() != null ? "?" + uri.getQuery() : "");
    // Prepare the HTTP request.
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query);
    request.setHeader(HttpHeaders.Names.HOST, host);
    LOG.info("Fetch: " + request.getUri());
    request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

    // Send the HTTP request.
    channel.write(request);

    // Wait for the server to close the connection.
    channel.getCloseFuture().awaitUninterruptibly();

    // Shut down executor threads to exit.
    bootstrap.releaseExternalResources();

    return file;
  }
Esempio n. 7
0
 /**
  * Inserts a header into the request. The {@link Object#toString()} method will be called on
  * {@code value} to determine the String value to actually use for the header value.
  *
  * <p>
  *
  * @return A reference to this, so multiple method calls can be chained.
  */
 public HttpClientRequest putHeader(String key, Object value) {
   check();
   request.setHeader(key, value);
   checkContentLengthChunked(key, value);
   return this;
 }
Esempio n. 8
0
 /**
  * adds a TxnId if one is not already set.
  *
  * @param request
  * @return
  */
 public static HttpRequest addTxnId(HttpRequest request) {
   if (request.getHeader("Strest-Txn-Id") == null)
     request.setHeader("Strest-Txn-Id", generateTxnId());
   return request;
 }
 /**
  * Finalize the request by preparing the Header in the request and returns the request ready to be
  * sent.<br>
  * Once finalized, no data must be added.<br>
  * If the request does not need chunk (isChunked() == false), this request is the only object to
  * send to the remote server.
  *
  * @return the request object (chunked or not according to size of body)
  * @throws ErrorDataEncoderException if the encoding is in error or if the finalize were already
  *     done
  */
 public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
   // Finalize the multipartHttpDatas
   if (!headerFinalized) {
     if (isMultipart) {
       InternalAttribute internal = new InternalAttribute();
       if (duringMixedMode) {
         internal.addValue("\r\n--" + multipartMixedBoundary + "--");
       }
       internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
       multipartHttpDatas.add(internal);
       multipartMixedBoundary = null;
       currentFileUpload = null;
       duringMixedMode = false;
       globalBodySize += internal.size();
     }
     headerFinalized = true;
   } else {
     throw new ErrorDataEncoderException("Header already encoded");
   }
   List<String> contentTypes = request.getHeaders(HttpHeaders.Names.CONTENT_TYPE);
   List<String> transferEncoding = request.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
   if (contentTypes != null) {
     request.removeHeader(HttpHeaders.Names.CONTENT_TYPE);
     for (String contentType : contentTypes) {
       // "multipart/form-data; boundary=--89421926422648"
       if (contentType.toLowerCase().startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA)) {
         // ignore
       } else if (contentType
           .toLowerCase()
           .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
         // ignore
       } else {
         request.addHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
       }
     }
   }
   if (isMultipart) {
     String value =
         HttpHeaders.Values.MULTIPART_FORM_DATA
             + "; "
             + HttpHeaders.Values.BOUNDARY
             + "="
             + multipartDataBoundary;
     request.addHeader(HttpHeaders.Names.CONTENT_TYPE, value);
   } else {
     // Not multipart
     request.addHeader(
         HttpHeaders.Names.CONTENT_TYPE, HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
   }
   // Now consider size for chunk or not
   long realSize = globalBodySize;
   if (isMultipart) {
     iterator = multipartHttpDatas.listIterator();
   } else {
     realSize -= 1; // last '&' removed
     iterator = multipartHttpDatas.listIterator();
   }
   request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(realSize));
   if (realSize > HttpPostBodyUtil.chunkSize) {
     isChunked = true;
     if (transferEncoding != null) {
       request.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
       for (String v : transferEncoding) {
         if (v.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
           // ignore
         } else {
           request.addHeader(HttpHeaders.Names.TRANSFER_ENCODING, v);
         }
       }
     }
     request.addHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
     request.setContent(ChannelBuffers.EMPTY_BUFFER);
   } else {
     // get the only one body and set it to the request
     HttpChunk chunk = nextChunk();
     request.setContent(chunk.getContent());
   }
   return request;
 }