Example usage for org.apache.http.params CoreProtocolPNames WAIT_FOR_CONTINUE

List of usage examples for org.apache.http.params CoreProtocolPNames WAIT_FOR_CONTINUE

Introduction

In this page you can find the example usage for org.apache.http.params CoreProtocolPNames WAIT_FOR_CONTINUE.

Prototype

String WAIT_FOR_CONTINUE

To view the source code for org.apache.http.params CoreProtocolPNames WAIT_FOR_CONTINUE.

Click Source Link

Usage

From source file:org.apache.abdera2.common.protocol.RequestHelper.java

public static HttpUriRequest createRequest(String method, String uri, HttpEntity entity,
        RequestOptions options) {/*  ww  w .  ja  va  2 s .c  om*/
    if (method == null)
        return null;
    if (options == null)
        options = createAtomDefaultRequestOptions().get();
    Method m = Method.get(method);
    Method actual = null;
    HttpUriRequest httpMethod = null;
    if (options.isUsePostOverride() && !nopostoveride.contains(m)) {
        actual = m;
        m = Method.POST;
    }
    if (m == GET)
        httpMethod = new HttpGet(uri);
    else if (m == POST) {
        httpMethod = new HttpPost(uri);
        if (entity != null)
            ((HttpPost) httpMethod).setEntity(entity);
    } else if (m == PUT) {
        httpMethod = new HttpPut(uri);
        if (entity != null)
            ((HttpPut) httpMethod).setEntity(entity);
    } else if (m == DELETE)
        httpMethod = new HttpDelete(uri);
    else if (m == HEAD)
        httpMethod = new HttpHead(uri);
    else if (m == OPTIONS)
        httpMethod = new HttpOptions(uri);
    else if (m == TRACE)
        httpMethod = new HttpTrace(uri);
    //        else if (m == PATCH)
    //          httpMethod = new ExtensionRequest(m.name(),uri,entity);
    else
        httpMethod = new ExtensionRequest(m.name(), uri, entity);
    if (actual != null) {
        httpMethod.addHeader("X-HTTP-Method-Override", actual.name());
    }
    initHeaders(options, httpMethod);
    HttpParams params = httpMethod.getParams();
    if (!options.isUseExpectContinue()) {
        params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    } else {
        if (options.getWaitForContinue() > -1)
            params.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, options.getWaitForContinue());
    }
    if (!(httpMethod instanceof HttpEntityEnclosingRequest))
        params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, options.isFollowRedirects());
    return httpMethod;
}

From source file:org.opendatakit.http.conn.GaeManagedClientConnection.java

@Override
public void flush() throws IOException {
    // flush is always called by 
    // org.apache.http.protocol.HttpRequestExecutor.doSendRequest

    // Build and issue the URLFetch request here.
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();

    boolean redirect = HttpClientParams.isRedirecting(params);
    @SuppressWarnings("unused")
    boolean authenticate = HttpClientParams.isAuthenticating(params);
    // TODO: verify that authentication is handled by URLFetchService...

    // default is to throw an exception on a overly-large request
    // follow redirects (e.g., to https), and to validate server
    // certificates.
    com.google.appengine.api.urlfetch.FetchOptions f = com.google.appengine.api.urlfetch.FetchOptions.Builder
            .withDefaults();/*from w w  w .j  a  v a2  s.  co  m*/
    f.disallowTruncate();
    f.validateCertificate();
    if (redirect) {
        f.followRedirects();
    } else {
        f.doNotFollowRedirects();
    }

    // set a deadline if we have a wait-for-continue limit
    // in an expectContinue situation 
    // or a timeout value set on the connection.
    HttpParams params = request.getParams();
    int deadline = 0;
    int msWaitForContinue = params.getIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 2000);
    if (expectContinueHeaders == null) {
        msWaitForContinue = 0;
    }
    int soTimeout = org.apache.http.params.HttpConnectionParams.getSoTimeout(params);
    int connTimeout = org.apache.http.params.HttpConnectionParams.getConnectionTimeout(params);
    if (soTimeout <= 0 || connTimeout <= 0) {
        deadline = 0; // wait forever...
    } else {
        int maxDelay = Math.max(Math.max(timeoutMilliseconds, msWaitForContinue), connTimeout);
        deadline = soTimeout + maxDelay;
    }

    if (deadline > 0) {
        logger.info("URLFetch timeout (socket + connection) (ms): " + deadline);
        f.setDeadline(new Double(0.001 * (double) deadline));
    }
    f.validateCertificate();

    com.google.appengine.api.urlfetch.HTTPMethod method;
    if (request instanceof HttpGet) {
        method = HTTPMethod.GET;
    } else if (request instanceof HttpPut) {
        method = HTTPMethod.PUT;
    } else if (request instanceof HttpPost) {
        method = HTTPMethod.POST;
    } else if (request instanceof HttpHead) {
        method = HTTPMethod.HEAD;
    } else if (request instanceof HttpDelete) {
        method = HTTPMethod.DELETE;
    } else if (request instanceof EntityEnclosingRequestWrapper) {
        String name = ((EntityEnclosingRequestWrapper) request).getMethod();
        method = HTTPMethod.valueOf(name);
    } else if (request instanceof RequestWrapper) {
        String name = ((RequestWrapper) request).getMethod();
        method = HTTPMethod.valueOf(name);
    } else {
        throw new IllegalStateException("Unrecognized Http request method");
    }

    // we need to construct the URL for the request
    // to the target host.  The request line, for, e.g., 
    // a get, needs to be added to the URL.
    URL url = new URL(targetHost.getSchemeName(), targetHost.getHostName(), targetHost.getPort(),
            request.getRequestLine().getUri());

    com.google.appengine.api.urlfetch.HTTPRequest req = new com.google.appengine.api.urlfetch.HTTPRequest(url,
            method, f);

    Header[] headers = request.getAllHeaders();
    for (Header h : headers) {
        req.addHeader(new com.google.appengine.api.urlfetch.HTTPHeader(h.getName(), h.getValue()));
    }
    // restore the expect-continue header
    if (expectContinueHeaders != null) {
        for (Header h : expectContinueHeaders) {
            req.addHeader(new com.google.appengine.api.urlfetch.HTTPHeader(h.getName(), h.getValue()));
        }
    }

    // see if we need to copy entity body over...
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        if (entity != null) {
            ByteArrayOutputStream blobStream = new ByteArrayOutputStream();
            entity.writeTo(blobStream);
            req.setPayload(blobStream.toByteArray());
        }
    }

    response = service.fetch(req);
}

From source file:com.archivas.clienttools.arcutils.impl.adapter.Hcap2Adapter.java

/**
 * @inheritDoc//from ww w .  j  a  v  a 2s.c o  m
 */
public void writeObjectFromStream(final String targetNode, final String targetPath, final InputStream is,
        final FileMetadata ingestionMetadata) throws StorageAdapterException {
    HttpHost httpHost = new HttpHost(targetNode, profile.getPort(), profile.getProtocol());
    String filePath = targetPath;

    if (!filePath.startsWith(HttpGatewayConstants.METADATA_MOUNT_URL_DIR)) {
        filePath = getProfile().resolvePath(filePath);
    }

    String queryString = null;
    if (ingestionMetadata != null) {
        queryString = generateQueryParameters(ingestionMetadata, false);
    }

    URI uri = null;
    try {
        uri = URIUtils.createURI(profile.getProtocol(), targetNode, profile.getPort(), filePath, queryString,
                null);
    } catch (URISyntaxException e) {
        LOG.log(Level.WARNING, "Unexpected error generating put URI for : " + targetPath);
        throw new StorageAdapterLiteralException("Error writing object to the server", e);
    }
    HttpPut request = new HttpPut(uri);

    InputStreamEntity isEntity = new InputStreamEntity(is, -1);
    request.setEntity(isEntity);

    request.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.TRUE);
    request.getParams().setParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 100);

    // Eventually we will just return this cookie which will be passed back to the caller.
    HcapAdapterCookie cookie = new HcapAdapterCookie(request, httpHost);
    synchronized (savingCookieLock) {
        if (savedCookie != null) {
            throw new RuntimeException(
                    "This adapter already has a current connection to host -- cannot create two at once.");
        }
        savedCookie = cookie;
    }
    try {
        executeMethod(cookie);
        this.handleHttpResponse(cookie.getResponse(), "writing", targetPath);
    } catch (IOException e) {
        this.handleIOExceptionFromRequest(e, "writing", targetPath);
    } finally {
        close();
    }
}

From source file:com.archivas.clienttools.arcutils.impl.adapter.Hcp3AuthNamespaceAdapter.java

public int reallyDoWriteStream(final String targetNode, String targetFile, final String queryString,
        final InputStream is, final String activity, final Header contentTypeHeader)
        throws StorageAdapterLiteralException, StorageAdapterException {
    int statusCode = -1;
    HttpHost httpHost = new HttpHost(targetNode, profile.getPort(), profile.getProtocol());
    URI uri;//from w w w. j a  v a 2 s  .  c o m
    try {
        String resolvedPath = getProfile().resolvePath(targetFile);
        uri = URIUtils.createURI(profile.getProtocol(), targetNode, profile.getPort(), resolvedPath,
                queryString, null);
    } catch (URISyntaxException e) {
        LOG.log(Level.WARNING, "Unexpected error generating put URI for : " + targetFile);
        throw new StorageAdapterLiteralException("Error writing object to the server", e);
    }

    HttpPut request = new HttpPut(uri);

    if (contentTypeHeader != null) {
        request.setHeader(contentTypeHeader);
    }

    try {
        InputStreamEntity isEntity = new InputStreamEntity(is, -1);
        request.setEntity(isEntity);

        request.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.TRUE);
        request.getParams().setParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 100);

        // Eventually we will just return this cookie which will be passed back to the caller.
        HcapAdapterCookie cookie = new HcapAdapterCookie(request, httpHost);
        synchronized (savingCookieLock) {
            if (savedCookie != null) {
                throw new RuntimeException(
                        "This adapter already has a current connection to host -- cannot create two at once.");
            }
            savedCookie = cookie;
        }
        executeMethod(cookie);

        this.handleHttpResponse(cookie.getResponse(), activity, targetFile);
    } catch (IOException e) {
        this.handleIOExceptionFromRequest(e, activity, targetFile);
    } finally {
        close();
    }

    return statusCode;
}

From source file:org.apache.http.impl.nio.client.NHttpClientProtocolHandler.java

public void requestReady(final NHttpClientConnection conn) {
    HttpContext context = conn.getContext();
    HttpExchange httpexchange = getHttpExchange(context);
    HttpAsyncExchangeHandler<?> handler = getHandler(context);
    if (this.log.isDebugEnabled()) {
        this.log.debug("Request ready " + formatState(conn, httpexchange));
    }//from   w ww  .j av a 2 s . com
    if (httpexchange.getRequestState() != MessageState.READY) {
        return;
    }
    if (handler == null || handler.isDone()) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("No request submitted " + formatState(conn, httpexchange));
        }
        return;
    }
    try {
        HttpRequest request = handler.generateRequest();
        httpexchange.setRequest(request);

        HttpEntityEnclosingRequest entityReq = null;
        if (request instanceof HttpEntityEnclosingRequest) {
            entityReq = (HttpEntityEnclosingRequest) request;
        }

        conn.submitRequest(request);

        if (entityReq != null) {
            if (entityReq.expectContinue()) {
                int timeout = conn.getSocketTimeout();
                httpexchange.setTimeout(timeout);
                timeout = request.getParams().getIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 3000);
                conn.setSocketTimeout(timeout);
                httpexchange.setRequestState(MessageState.ACK);
            } else {
                httpexchange.setRequestState(MessageState.BODY_STREAM);
            }
        } else {
            httpexchange.setRequestState(MessageState.COMPLETED);
        }
    } catch (IOException ex) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("I/O error: " + ex.getMessage(), ex);
        }
        shutdownConnection(conn);
        handler.failed(ex);
    } catch (HttpException ex) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("HTTP protocol exception: " + ex.getMessage(), ex);
        }
        closeConnection(conn);
        handler.failed(ex);
    }
}