Example usage for org.apache.http.protocol HttpContext setAttribute

List of usage examples for org.apache.http.protocol HttpContext setAttribute

Introduction

In this page you can find the example usage for org.apache.http.protocol HttpContext setAttribute.

Prototype

void setAttribute(String str, Object obj);

Source Link

Usage

From source file:org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher.java

public boolean updateToStore(API api, APIStore store) throws APIManagementException {
    boolean updated = false;
    if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
        String msg = "External APIStore endpoint URL or credentials are not defined.Cannot proceed with "
                + "publishing API to the APIStore - " + store.getDisplayName();
        throw new APIManagementException(msg);
    } else {/*  w  ww .  j  ava 2 s .  c om*/
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        boolean authenticated = authenticateAPIM(store, httpContext);
        if (authenticated) {
            updated = updateWSO2Store(api, store.getUsername(), store.getEndpoint(), httpContext,
                    store.getDisplayName());
            logoutFromExternalStore(store, httpContext);
        }
        return updated;
    }
}

From source file:org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher.java

@Override
public boolean deleteFromStore(APIIdentifier apiId, APIStore store) throws APIManagementException {
    boolean deleted = false;
    if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
        String msg = "External APIStore endpoint URL or credentials are not defined. "
                + "Cannot proceed with publishing API to the APIStore - " + store.getDisplayName();
        throw new APIManagementException(msg);
    } else {/*  w w  w . j a  va2s.  c  om*/
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        boolean authenticated = authenticateAPIM(store, httpContext);
        if (authenticated) {
            deleted = deleteWSO2Store(apiId, store.getUsername(), store.getEndpoint(), httpContext,
                    store.getDisplayName());
            logoutFromExternalStore(store, httpContext);
        }
        return deleted;
    }
}

From source file:org.zenoss.app.consumer.metric.impl.MetricServicePoster.java

/**
 * Login to the ZAuth service using the zenoss credentials.  Grab the tenant id from the http response and cache
 * the results for future requests.//  w ww . j  a  v  a  2s  .  c o  m
 * @return tenant id
 * @throws IOException
 */
String getTenantId() throws IOException {
    if (tenantId == null) {
        log.debug("Requesting tenant id");
        // get the hostname and port from ProxyConfiguration
        ProxyConfiguration proxyConfig = configuration.getProxyConfiguration();
        String hostname = proxyConfig.getHostname();
        int port = proxyConfig.getPort();

        //configure request
        HttpContext context = new BasicHttpContext();
        HttpHost host = new HttpHost(hostname, port, "http");
        HttpPost post = new HttpPost(AUTHENTICATE_URL);
        post.addHeader(ACCEPT, APPLICATION_JSON.toString());

        //configure authentication
        String username = configuration.getZenossCredentials().getUsername();
        String password = configuration.getZenossCredentials().getPassword();
        if (!Strings.nullToEmpty(username).isEmpty()) {
            //configure credentials
            CredentialsProvider provider = new BasicCredentialsProvider();
            provider.setCredentials(new AuthScope(host.getHostName(), host.getPort()),
                    new UsernamePasswordCredentials(username, password));
            context.setAttribute(ClientContext.CREDS_PROVIDER, provider);

            //setup auth cache
            AuthCache cache = new BasicAuthCache();
            BasicScheme scheme = new BasicScheme();
            cache.put(host, scheme);
            context.setAttribute(ClientContext.AUTH_CACHE, cache);
        }

        //handle response and collect tenantId
        HttpResponse response = null;
        try {
            response = httpClient.execute(host, post, context);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            log.debug("Tenant id request complete with status: {}", statusCode);
            if (statusCode >= 200 && statusCode <= 299) {
                Header id = response.getFirstHeader(ZenossTenant.ID_HTTP_HEADER);
                if (id == null) {
                    log.warn("Failed to get zauth tenant id after login");
                    throw new RuntimeException("Failed to get zauth tenant id after successful login");
                }
                tenantId = id.getValue();
                log.info("Got tenant id: {}", tenantId);
            } else {
                log.warn("Unsuccessful response from server: {}", response.getStatusLine());
                throw new IOException("Login for tenantId failed");
            }
        } catch (NullPointerException ex) {
            log.warn("npe retrieving tenantId: {}", ex);
        } finally {
            try {
                if (response != null) {
                    response.getEntity().getContent().close();
                }
            } catch (NullPointerException | IOException ex) {
                log.warn("Failed to close request: {}", ex);
            }
        }
    }
    return tenantId;
}

From source file:org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher.java

/**
 * The method to publish API to external WSO2 Store
 * @param api      API/*from w  w w  .j  av  a 2 s.c o  m*/
 * @param store    Store
 * @return   published/not
 */

public boolean publishToStore(API api, APIStore store) throws APIManagementException {
    boolean published = false;

    if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
        String msg = "External APIStore endpoint URL or credentials are not defined. "
                + "Cannot proceed with publishing API to the APIStore - " + store.getDisplayName();
        throw new APIManagementException(msg);
    } else {
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        boolean authenticated = authenticateAPIM(store, httpContext);
        if (authenticated) { //First try to login to store
            boolean added = addAPIToStore(api, store.getEndpoint(), store.getUsername(), httpContext,
                    store.getDisplayName());
            if (added) { //If API creation success,then try publishing the API
                published = publishAPIToStore(api.getId(), store.getEndpoint(), store.getUsername(),
                        httpContext, store.getDisplayName());
            }
            logoutFromExternalStore(store, httpContext);
        }
    }
    return published;
}

From source file:org.wso2.carbon.apimgt.impl.publishers.WSO2APIPublisher.java

@Override
public boolean createVersionedAPIToStore(API api, APIStore store, String version)
        throws APIManagementException {
    boolean published = false;

    if (store.getEndpoint() == null || store.getUsername() == null || store.getPassword() == null) {
        String msg = "External APIStore endpoint URL or credentials are not defined. Cannot proceed with "
                + "publishing API to the APIStore - " + store.getDisplayName();
        throw new APIManagementException(msg);
    } else {/*w  w  w.  j ava 2 s  .co m*/
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        boolean authenticated = authenticateAPIM(store, httpContext);
        if (authenticated) { //First try to login to store
            boolean added = addVersionedAPIToStore(api, store.getEndpoint(), version, httpContext,
                    store.getDisplayName(), store.getUsername());
            if (added) { //If API creation success,then try publishing the API
                published = publishAPIToStore(api.getId(), store.getEndpoint(), store.getUsername(),
                        httpContext, store.getDisplayName());
            }
            logoutFromExternalStore(store, httpContext);
        }
    }
    return published;

}

From source file:self.philbrown.javaQuery.AjaxTask.java

@Override
protected TaskResponse doInBackground(Void... arg0) {
    //handle cached responses
    CachedResponse cachedResponse = URLresponses
            .get(String.format(Locale.US, "%s_?=%s", options.url(), options.dataType()));
    //handle ajax caching option
    if (cachedResponse != null) {
        if (options.cache()) {
            if (new Date().getTime() - cachedResponse.timestamp.getTime() < options.cacheTimeout()) {
                //return cached response
                Success s = new Success();
                s.obj = cachedResponse.response;
                s.reason = "cached response";
                s.headers = null;//from   w ww  . ja va  2 s .  c  o m
                return s;
            }
        }

    }

    if (request == null) {
        String type = options.type();
        if (type == null)
            type = "GET";
        if (type.equalsIgnoreCase("DELETE")) {
            request = new HttpDelete(options.url());
        } else if (type.equalsIgnoreCase("GET")) {
            request = new HttpGet(options.url());
        } else if (type.equalsIgnoreCase("HEAD")) {
            request = new HttpHead(options.url());
        } else if (type.equalsIgnoreCase("OPTIONS")) {
            request = new HttpOptions(options.url());
        } else if (type.equalsIgnoreCase("POST")) {
            request = new HttpPost(options.url());
        } else if (type.equalsIgnoreCase("PUT")) {
            request = new HttpPut(options.url());
        } else if (type.equalsIgnoreCase("TRACE")) {
            request = new HttpTrace(options.url());
        } else if (type.equalsIgnoreCase("CUSTOM")) {
            try {
                request = options.customRequest();
            } catch (Exception e) {
                request = null;
            }

            if (request == null) {
                Log.w("javaQuery.ajax",
                        "CUSTOM type set, but AjaxOptions.customRequest is invalid. Defaulting to GET.");
                request = new HttpGet();
            }

        } else {
            //default to GET
            request = new HttpGet();
        }
    }

    Map<String, Object> args = new HashMap<String, Object>();
    args.put("options", options);
    args.put("request", request);
    EventCenter.trigger("ajaxPrefilter", args, null);

    if (options.headers() != null) {
        if (options.headers().authorization() != null) {
            options.headers()
                    .authorization(options.headers().authorization() + " " + options.getEncodedCredentials());
        } else if (options.username() != null) {
            //guessing that authentication is basic
            options.headers().authorization("Basic " + options.getEncodedCredentials());
        }

        for (Entry<String, String> entry : options.headers().map().entrySet()) {
            request.addHeader(entry.getKey(), entry.getValue());
        }
    }

    if (options.data() != null) {
        try {
            Method setEntity = request.getClass().getMethod("setEntity", new Class<?>[] { HttpEntity.class });
            if (options.processData() == null) {
                setEntity.invoke(request, new StringEntity(options.data().toString()));
            } else {
                Class<?> dataProcessor = Class.forName(options.processData());
                Constructor<?> constructor = dataProcessor.getConstructor(new Class<?>[] { Object.class });
                setEntity.invoke(request, constructor.newInstance(options.data()));
            }
        } catch (Throwable t) {
            Log.w("Ajax", "Could not post data");
        }
    }

    HttpParams params = new BasicHttpParams();

    if (options.timeout() != 0) {
        HttpConnectionParams.setConnectionTimeout(params, options.timeout());
        HttpConnectionParams.setSoTimeout(params, options.timeout());
    }

    HttpClient client = new DefaultHttpClient(params);

    HttpResponse response = null;
    try {

        if (options.cookies() != null) {
            CookieStore cookies = new BasicCookieStore();
            for (Entry<String, String> entry : options.cookies().entrySet()) {
                cookies.addCookie(new BasicClientCookie(entry.getKey(), entry.getValue()));
            }
            HttpContext httpContext = new BasicHttpContext();
            httpContext.setAttribute(ClientContext.COOKIE_STORE, cookies);
            response = client.execute(request, httpContext);
        } else {
            response = client.execute(request);
        }

        if (options.dataFilter() != null) {
            if (options.context() != null)
                options.dataFilter().invoke(new $(options.context()), response, options.dataType());
            else
                options.dataFilter().invoke(null, response, options.dataType());
        }

        StatusLine statusLine = response.getStatusLine();

        Function function = options.statusCode().get(statusLine);
        if (function != null) {
            if (options.context() != null)
                function.invoke(new $(options.context()));
            else
                function.invoke(null);
        }

        if (statusLine.getStatusCode() >= 300) {
            //an error occurred
            Error e = new Error();
            AjaxError error = new AjaxError();
            error.request = request;
            error.options = options;
            e.status = statusLine.getStatusCode();
            e.reason = statusLine.getReasonPhrase();
            error.status = e.status;
            error.reason = e.reason;
            e.headers = response.getAllHeaders();
            e.error = error;
            return e;
        } else {
            //handle dataType
            String dataType = options.dataType();
            if (dataType == null)
                dataType = "text";
            Object parsedResponse = null;
            boolean success = true;
            try {
                if (dataType.equalsIgnoreCase("text") || dataType.equalsIgnoreCase("html")) {
                    parsedResponse = parseText(response);
                } else if (dataType.equalsIgnoreCase("xml")) {
                    if (options.customXMLParser() != null) {
                        InputStream is = response.getEntity().getContent();
                        if (options.SAXContentHandler() != null)
                            options.customXMLParser().parse(is, options.SAXContentHandler());
                        else
                            options.customXMLParser().parse(is, new DefaultHandler());
                        parsedResponse = "Response handled by custom SAX parser";
                    } else if (options.SAXContentHandler() != null) {
                        InputStream is = response.getEntity().getContent();

                        SAXParserFactory factory = SAXParserFactory.newInstance();

                        factory.setFeature("http://xml.org/sax/features/namespaces", false);
                        factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);

                        SAXParser parser = factory.newSAXParser();

                        XMLReader reader = parser.getXMLReader();
                        reader.setContentHandler(options.SAXContentHandler());
                        reader.parse(new InputSource(is));
                        parsedResponse = "Response handled by custom SAX content handler";
                    } else {
                        parsedResponse = parseXML(response);
                    }
                } else if (dataType.equalsIgnoreCase("json")) {
                    parsedResponse = parseJSON(response);
                } else if (dataType.equalsIgnoreCase("script")) {
                    parsedResponse = parseScript(response);
                } else if (dataType.equalsIgnoreCase("image")) {
                    parsedResponse = parseImage(response);
                }
            } catch (ClientProtocolException cpe) {
                if (options.debug())
                    cpe.printStackTrace();
                success = false;
                Error e = new Error();
                AjaxError error = new AjaxError();
                error.request = request;
                error.options = options;
                e.status = statusLine.getStatusCode();
                e.reason = statusLine.getReasonPhrase();
                error.status = e.status;
                error.reason = e.reason;
                e.headers = response.getAllHeaders();
                e.error = error;
                return e;
            } catch (Exception ioe) {
                if (options.debug())
                    ioe.printStackTrace();
                success = false;
                Error e = new Error();
                AjaxError error = new AjaxError();
                error.request = request;
                error.options = options;
                e.status = statusLine.getStatusCode();
                e.reason = statusLine.getReasonPhrase();
                error.status = e.status;
                error.reason = e.reason;
                e.headers = response.getAllHeaders();
                e.error = error;
                return e;
            }
            if (success) {
                //Handle cases where successful requests still return errors (these include
                //configurations in AjaxOptions and HTTP Headers
                String key = String.format(Locale.US, "%s_?=%s", options.url(), options.dataType());
                CachedResponse cache = URLresponses.get(key);
                Date now = new Date();
                //handle ajax caching option
                if (cache != null) {
                    if (options.cache()) {
                        if (now.getTime() - cache.timestamp.getTime() < options.cacheTimeout()) {
                            parsedResponse = cache;
                        } else {
                            cache.response = parsedResponse;
                            cache.timestamp = now;
                            synchronized (URLresponses) {
                                URLresponses.put(key, cache);
                            }
                        }
                    }

                }
                //handle ajax ifModified option
                Header[] lastModifiedHeaders = response.getHeaders("last-modified");
                if (lastModifiedHeaders.length >= 1) {
                    try {
                        Header h = lastModifiedHeaders[0];
                        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
                        Date lastModified = format.parse(h.getValue());
                        if (options.ifModified() && lastModified != null) {
                            if (cache.lastModified != null && cache.lastModified.compareTo(lastModified) == 0) {
                                //request response has not been modified. 
                                //Causes an error instead of a success.
                                Error e = new Error();
                                AjaxError error = new AjaxError();
                                error.request = request;
                                error.options = options;
                                e.status = statusLine.getStatusCode();
                                e.reason = statusLine.getReasonPhrase();
                                error.status = e.status;
                                error.reason = e.reason;
                                e.headers = response.getAllHeaders();
                                e.error = error;
                                Function func = options.statusCode().get(304);
                                if (func != null) {
                                    if (options.context() != null)
                                        func.invoke(new $(options.context()));
                                    else
                                        func.invoke(null);
                                }
                                return e;
                            } else {
                                cache.lastModified = lastModified;
                                synchronized (URLresponses) {
                                    URLresponses.put(key, cache);
                                }
                            }
                        }
                    } catch (Throwable t) {
                        Log.e("Ajax", "Could not parse Last-Modified Header");
                    }

                }

                //Now handle a successful request

                Success s = new Success();
                s.obj = parsedResponse;
                s.reason = statusLine.getReasonPhrase();
                s.headers = response.getAllHeaders();
                return s;
            }
            //success
            Success s = new Success();
            s.obj = parsedResponse;
            s.reason = statusLine.getReasonPhrase();
            s.headers = response.getAllHeaders();
            return s;
        }

    } catch (Throwable t) {
        if (options.debug())
            t.printStackTrace();
        if (t instanceof java.net.SocketTimeoutException) {
            Error e = new Error();
            AjaxError error = new AjaxError();
            error.request = request;
            error.options = options;
            e.status = 0;
            String reason = t.getMessage();
            if (reason == null)
                reason = "Socket Timeout";
            e.reason = reason;
            error.status = e.status;
            error.reason = e.reason;
            if (response != null)
                e.headers = response.getAllHeaders();
            else
                e.headers = new Header[0];
            e.error = error;
            return e;
        }
        return null;
    }
}

From source file:com.heaptrip.util.http.bixo.fetcher.SimpleHttpFetcher.java

private FetchedResult doRequest(HttpRequestBase request, String url, List<TupleTwo<?, ?>> data,
        List<TupleTwo<?, ?>> headers) throws BaseFetchException {
    LOGGER.trace("Fetching " + url);

    HttpResponse response;/*  w  w w . j a v a  2s .com*/
    long readStartTime;
    HttpHeaders headerMap = new HttpHeaders();
    String redirectedUrl = null;
    String newBaseUrl = null;
    int numRedirects = 0;
    boolean needAbort = true;
    String contentType = "";
    String hostAddress = null;

    // Create a local instance of cookie store, and bind to local context
    // Without this we get killed w/lots of threads, due to sync() on single
    // cookie store.
    HttpContext localContext = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    try {
        URI uri = new URI(url);
        request.setURI(uri);
        request.setHeader("Host", uri.getHost());

        if (headers != null) {
            for (TupleTwo<?, ?> t : headers) {
                request.setHeader(t.getKey().toString(), t.getValue().toString());
            }
        }

        //collect post data if available
        if (request instanceof HttpPost && data != null) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            for (TupleTwo<?, ?> e : data) {
                nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(e.getKey().toString(), "utf-8"),
                        URLEncoder.encode(e.getValue().toString(), "utf-8")));
            }
            ((HttpPost) (request)).setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }

        readStartTime = System.currentTimeMillis();
        response = _httpClient.execute(request, localContext);

        Header[] responseHeaders = response.getAllHeaders();
        for (Header header : responseHeaders) {
            headerMap.add(header.getName(), header.getValue());
        }

        int httpStatus = response.getStatusLine().getStatusCode();
        if ((httpStatus < 200) || (httpStatus >= 300)) {
            // We can't just check against SC_OK, as some wackos return 201, 202,
            // etc
            throw new HttpFetchException(url,
                    "Error fetching " + url + " due to http status code " + httpStatus, httpStatus, headerMap);
        }

        redirectedUrl = extractRedirectedUrl(url, localContext);

        URI permRedirectUri = (URI) localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY);
        if (permRedirectUri != null) {
            newBaseUrl = permRedirectUri.toURL().toExternalForm();
        }

        Integer redirects = (Integer) localContext.getAttribute(REDIRECT_COUNT_CONTEXT_KEY);
        if (redirects != null) {
            numRedirects = redirects.intValue();
        }

        hostAddress = (String) (localContext.getAttribute(HOST_ADDRESS));
        if (hostAddress == null) {
            throw new UrlFetchException(url, "Host address not saved in context");
        }

        Header cth = response.getFirstHeader(HttpHeaderNames.CONTENT_TYPE);
        if (cth != null) {
            contentType = cth.getValue();
        }

        needAbort = false;
    } catch (ClientProtocolException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException
        // (which is is a subclass of)
        needAbort = false;

        // If the root case was a "too many redirects" error, we want to map this
        // to a specific
        // exception that contains the final redirect.
        if (e.getCause() instanceof MyRedirectException) {
            MyRedirectException mre = (MyRedirectException) e.getCause();
            String redirectUrl = url;

            try {
                redirectUrl = mre.getUri().toURL().toExternalForm();
            } catch (MalformedURLException e2) {
                LOGGER.warn("Invalid URI saved during redirect handling: " + mre.getUri());
            }

            throw new RedirectFetchException(url, redirectUrl, mre.getReason());
        } else if (e.getCause() instanceof RedirectException) {
            throw new RedirectFetchException(url, extractRedirectedUrl(url, localContext),
                    RedirectExceptionReason.TOO_MANY_REDIRECTS);
        } else {
            throw new IOFetchException(url, e);
        }
    } catch (IOException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException
        needAbort = false;

        if (e instanceof ConnectionPoolTimeoutException) {
            // Should never happen, so let's dump some info about the connection
            // pool.
            ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) _httpClient.getConnectionManager();
            int numConnections = cm.getConnectionsInPool();
            cm.closeIdleConnections(0, TimeUnit.MILLISECONDS);
            LOGGER.error(String.format(
                    "Got ConnectionPoolTimeoutException: %d connections before, %d after idle close",
                    numConnections, cm.getConnectionsInPool()));
        }

        throw new IOFetchException(url, e);
    } catch (URISyntaxException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (IllegalStateException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (BaseFetchException e) {
        throw e;
    } catch (Exception e) {
        // Map anything else to a generic IOFetchException
        // TODO KKr - create generic fetch exception
        throw new IOFetchException(url, new IOException(e));
    } finally {
        safeAbort(needAbort, request);
    }

    // Figure out how much data we want to try to fetch.
    int targetLength = _fetcherPolicy.getMaxContentSize();
    boolean truncated = false;
    String contentLengthStr = headerMap.getFirst(HttpHeaderNames.CONTENT_LENGTH);
    if (contentLengthStr != null) {
        try {
            int contentLength = Integer.parseInt(contentLengthStr);
            if (contentLength > targetLength) {
                truncated = true;
            } else {
                targetLength = contentLength;
            }
        } catch (NumberFormatException e) {
            // Ignore (and log) invalid content length values.
            LOGGER.warn("Invalid content length in header: " + contentLengthStr);
        }
    }

    // Now finally read in response body, up to targetLength bytes.
    // Note that entity might be null, for zero length responses.
    byte[] content = new byte[0];
    long readRate = 0;
    HttpEntity entity = response.getEntity();
    needAbort = true;

    if (entity != null) {
        InputStream in = null;

        try {
            in = entity.getContent();
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = 0;
            int totalRead = 0;
            ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BYTEARRAY_SIZE);

            int readRequests = 0;
            int minResponseRate = _fetcherPolicy.getMinResponseRate();
            // TODO KKr - we need to monitor the rate while reading a
            // single block. Look at HttpClient
            // metrics support for how to do this. Once we fix this, fix
            // the test to read a smaller (< 20K)
            // chuck of data.
            while ((totalRead < targetLength) && ((bytesRead = in.read(buffer, 0,
                    Math.min(buffer.length, targetLength - totalRead))) != -1)) {
                readRequests += 1;
                totalRead += bytesRead;
                out.write(buffer, 0, bytesRead);

                // Assume read time is at least one millisecond, to avoid DBZ
                // exception.
                long totalReadTime = Math.max(1, System.currentTimeMillis() - readStartTime);
                readRate = (totalRead * 1000L) / totalReadTime;

                // Don't bail on the first read cycle, as we can get a hiccup starting
                // out.
                // Also don't bail if we've read everything we need.
                if ((readRequests > 1) && (totalRead < targetLength) && (readRate < minResponseRate)) {
                    throw new AbortedFetchException(url, "Slow response rate of " + readRate + " bytes/sec",
                            AbortedFetchReason.SLOW_RESPONSE_RATE);
                }

                // Check to see if we got interrupted.
                if (Thread.interrupted()) {
                    throw new AbortedFetchException(url, AbortedFetchReason.INTERRUPTED);
                }
            }

            content = out.toByteArray();
            needAbort = truncated || (in.available() > 0);
        } catch (IOException e) {
            // We don't need to abort if there's an IOException
            throw new IOFetchException(url, e);
        } finally {
            safeAbort(needAbort, request);
            safeClose(in);
        }
    }

    return new FetchedResult(url, redirectedUrl, System.currentTimeMillis(), headerMap, content, contentType,
            (int) readRate, newBaseUrl, numRedirects, hostAddress);
}

From source file:de.micromata.genome.tpsb.soapui.DelegateToSoapUiTestBuilderHttpClientRequestTransport.java

@Override
public Response sendRequest(SubmitContext submitContext, Request request) throws Exception {
    boolean useSuper = false;

    if (useSuper == true) {
        return super.sendRequest(submitContext, request);
    }//from www. ja  va  2 s .co m
    AbstractHttpRequestInterface<?> httpRequest = (AbstractHttpRequestInterface<?>) request;
    RequestMethod rm = httpRequest.getMethod();

    ExtendedHttpMethod httpMethod;
    switch (rm) {
    case POST: {
        ExtendedPostMethod extendedPostMethod = new ExtendedPostMethod();
        extendedPostMethod.setAfterRequestInjection(httpRequest.getAfterRequestInjection());
        httpMethod = extendedPostMethod;
        break;
    }
    case GET: {
        ExtendedGetMethod extendedGetMethod = new ExtendedGetMethod();
        //        extendedGetMethod.setAfterRequestInjection(httpRequest.getAfterRequestInjection());
        httpMethod = extendedGetMethod;
        break;
    }
    default:
        throw new IllegalArgumentException("Unsupported HTTP methd: " + rm);
    }

    HttpClientSupport.SoapUIHttpClient httpClient = HttpClientSupport.getHttpClient();

    boolean createdContext = false;
    HttpContext httpContext = (HttpContext) submitContext.getProperty(SubmitContext.HTTP_STATE_PROPERTY);
    if (httpContext == null) {
        httpContext = new BasicHttpContext();
        submitContext.setProperty(SubmitContext.HTTP_STATE_PROPERTY, httpContext);
        createdContext = true;

        // always use local cookie store so we don't share cookies with other threads/executions/requests
        CookieStore cookieStore = new BasicCookieStore();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    }

    String localAddress = System.getProperty("soapui.bind.address", httpRequest.getBindAddress());
    if (localAddress == null || localAddress.trim().length() == 0) {
        localAddress = SoapUI.getSettings().getString(HttpSettings.BIND_ADDRESS, null);
    }

    org.apache.http.HttpResponse httpResponse = null;
    if (localAddress != null && localAddress.trim().length() > 0) {
        try {
            httpMethod.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS,
                    InetAddress.getByName(localAddress));
        } catch (Exception e) {
            SoapUI.logError(e, "Failed to set localAddress to [" + localAddress + "]");
        }
    }
    Map<String, String> httpRequestParameter = new HashMap<>();
    if (httpRequest instanceof HttpTestRequest) {
        HttpTestRequest tr = (HttpTestRequest) httpRequest;
        for (String key : tr.getParams().keySet()) {
            RestParamProperty val = tr.getParams().get(key);
            String sval = val.getValue();
            sval = PropertyExpander.expandProperties(submitContext, sval);
            httpRequestParameter.put(key, sval);
        }
    }

    submitContext.removeProperty(RESPONSE);
    submitContext.setProperty(HTTP_METHOD, httpMethod);
    submitContext.setProperty(POST_METHOD, httpMethod);
    submitContext.setProperty(HTTP_CLIENT, httpClient);
    submitContext.setProperty(REQUEST_CONTENT, httpRequest.getRequestContent());
    submitContext.setProperty(WSDL_REQUEST, httpRequest);
    submitContext.setProperty(RESPONSE_PROPERTIES, new StringToStringMap());
    List<RequestFilter> filters = getFilters();
    for (RequestFilter filter : filters) {
        filter.filterRequest(submitContext, httpRequest);
    }

    try {
        Settings settings = httpRequest.getSettings();

        // custom http headers last so they can be overridden
        StringToStringsMap headers = httpRequest.getRequestHeaders();

        // first remove so we don't get any unwanted duplicates
        for (String header : headers.keySet()) {
            httpMethod.removeHeaders(header);
        }

        // now add
        for (String header : headers.keySet()) {
            for (String headerValue : headers.get(header)) {
                headerValue = PropertyExpander.expandProperties(submitContext, headerValue);
                httpMethod.addHeader(header, headerValue);
            }
        }

        // do request
        WsdlProject project = (WsdlProject) ModelSupport.getModelItemProject(httpRequest);
        WssCrypto crypto = null;
        if (project != null && project.getWssContainer() != null) {
            crypto = project.getWssContainer().getCryptoByName(
                    PropertyExpander.expandProperties(submitContext, httpRequest.getSslKeystore()));
        }

        if (crypto != null && WssCrypto.STATUS_OK.equals(crypto.getStatus())) {
            httpMethod.getParams().setParameter(SoapUIHttpRoute.SOAPUI_SSL_CONFIG,
                    crypto.getSource() + " " + crypto.getPassword());
        }

        // dump file?
        httpMethod.setDumpFile(PathUtils.expandPath(httpRequest.getDumpFile(),
                (AbstractWsdlModelItem<?>) httpRequest, submitContext));

        // include request time?
        if (settings.getBoolean(HttpSettings.INCLUDE_REQUEST_IN_TIME_TAKEN)) {
            httpMethod.initStartTime();
        }

        if (httpMethod.getMetrics() != null) {
            httpMethod.getMetrics().setHttpMethod(httpMethod.getMethod());
            PrivateBeanUtils.invokeMethod(this, "captureMetrics", httpMethod, httpClient);
            httpMethod.getMetrics().getTotalTimer().start();
        }

        // submit!
        httpResponse = execute(submitContext, httpMethod, httpContext, httpRequestParameter);

        if (httpMethod.getMetrics() != null) {
            httpMethod.getMetrics().getReadTimer().stop();
            httpMethod.getMetrics().getTotalTimer().stop();
        }

        //          if (isRedirectResponse(httpResponse.getStatusLine().getStatusCode()) && httpRequest.isFollowRedirects()) {
        //            if (httpResponse.getEntity() != null) {
        //              EntityUtils.consume(httpResponse.getEntity());
        //            }
        //
        //            ExtendedGetMethod returnMethod = followRedirects(httpClient, 0, httpMethod, httpResponse, httpContext);
        //            httpMethod = returnMethod;
        //            submitContext.setProperty(HTTP_METHOD, httpMethod);
        //          }
    } catch (Throwable t) { // NOSONAR "Illegal Catch" framework
        httpMethod.setFailed(t);

        if (t instanceof Exception) {
            throw (Exception) t;
        }

        SoapUI.logError(t);
        throw new Exception(t);
    } finally {
        if (!httpMethod.isFailed()) {
            if (httpMethod.getMetrics() != null) {
                if (httpMethod.getMetrics().getReadTimer().getStop() == 0) {
                    httpMethod.getMetrics().getReadTimer().stop();
                }
                if (httpMethod.getMetrics().getTotalTimer().getStop() == 0) {
                    httpMethod.getMetrics().getTotalTimer().stop();
                }
            }
        } else {
            httpMethod.getMetrics().reset();
            httpMethod.getMetrics().setTimestamp(System.currentTimeMillis());
            //            captureMetrics(httpMethod, httpClient);
        }
        for (int c = filters.size() - 1; c >= 0; c--) {
            RequestFilter filter = filters.get(c);
            filter.afterRequest(submitContext, httpRequest);
        }

        if (!submitContext.hasProperty(RESPONSE)) {
            createDefaultResponse(submitContext, httpRequest, httpMethod);
        }

        Response response = (Response) submitContext.getProperty(BaseHttpRequestTransport.RESPONSE);
        StringToStringMap responseProperties = (StringToStringMap) submitContext
                .getProperty(BaseHttpRequestTransport.RESPONSE_PROPERTIES);

        for (String key : responseProperties.keySet()) {
            response.setProperty(key, responseProperties.get(key));
        }

        if (createdContext) {
            submitContext.setProperty(SubmitContext.HTTP_STATE_PROPERTY, null);
        }
    }

    return (Response) submitContext.getProperty(BaseHttpRequestTransport.RESPONSE);
}

From source file:bixo.fetcher.SimpleHttpFetcher.java

private FetchedResult doRequest(HttpRequestBase request, String url, List<Tuple2<?, ?>> data,
        List<Tuple2<?, ?>> headers) throws BaseFetchException {
    LOGGER.trace("Fetching " + url);

    HttpResponse response;//from   w w w.j  a va 2s .c  o m
    long readStartTime;
    HttpHeaders headerMap = new HttpHeaders();
    String redirectedUrl = null;
    String newBaseUrl = null;
    int numRedirects = 0;
    boolean needAbort = true;
    String contentType = "";
    String hostAddress = null;

    // Create a local instance of cookie store, and bind to local context
    // Without this we get killed w/lots of threads, due to sync() on single
    // cookie store.
    HttpContext localContext = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    try {
        URI uri = new URI(url);
        request.setURI(uri);
        request.setHeader("Host", uri.getHost());

        if (headers != null) {
            for (Tuple2<?, ?> t : headers) {
                request.setHeader(t.getKey().toString(), t.getValue().toString());
            }
        }

        //collect post data if available
        if (request instanceof HttpPost && data != null) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            for (Tuple2<?, ?> e : data) {
                nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(e.getKey().toString(), "utf-8"),
                        URLEncoder.encode(e.getValue().toString(), "utf-8")));
            }
            ((HttpPost) (request)).setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }

        readStartTime = System.currentTimeMillis();
        response = _httpClient.execute(request, localContext);

        Header[] responseHeaders = response.getAllHeaders();
        for (Header header : responseHeaders) {
            headerMap.add(header.getName(), header.getValue());
        }

        int httpStatus = response.getStatusLine().getStatusCode();
        if ((httpStatus < 200) || (httpStatus >= 300)) {
            // We can't just check against SC_OK, as some wackos return 201, 202,
            // etc
            throw new HttpFetchException(url,
                    "Error fetching " + url + " due to http status code " + httpStatus, httpStatus, headerMap);
        }

        redirectedUrl = extractRedirectedUrl(url, localContext);

        URI permRedirectUri = (URI) localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY);
        if (permRedirectUri != null) {
            newBaseUrl = permRedirectUri.toURL().toExternalForm();
        }

        Integer redirects = (Integer) localContext.getAttribute(REDIRECT_COUNT_CONTEXT_KEY);
        if (redirects != null) {
            numRedirects = redirects.intValue();
        }

        hostAddress = (String) (localContext.getAttribute(HOST_ADDRESS));
        if (hostAddress == null) {
            throw new UrlFetchException(url, "Host address not saved in context");
        }

        Header cth = response.getFirstHeader(HttpHeaderNames.CONTENT_TYPE);
        if (cth != null) {
            contentType = cth.getValue();
        }

        needAbort = false;
    } catch (ClientProtocolException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException
        // (which is is a subclass of)
        needAbort = false;

        // If the root case was a "too many redirects" error, we want to map this
        // to a specific
        // exception that contains the final redirect.
        if (e.getCause() instanceof MyRedirectException) {
            MyRedirectException mre = (MyRedirectException) e.getCause();
            String redirectUrl = url;

            try {
                redirectUrl = mre.getUri().toURL().toExternalForm();
            } catch (MalformedURLException e2) {
                LOGGER.warn("Invalid URI saved during redirect handling: " + mre.getUri());
            }

            throw new RedirectFetchException(url, redirectUrl, mre.getReason());
        } else if (e.getCause() instanceof RedirectException) {
            throw new RedirectFetchException(url, extractRedirectedUrl(url, localContext),
                    RedirectExceptionReason.TOO_MANY_REDIRECTS);
        } else {
            throw new IOFetchException(url, e);
        }
    } catch (IOException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException
        needAbort = false;

        if (e instanceof ConnectionPoolTimeoutException) {
            // Should never happen, so let's dump some info about the connection
            // pool.
            ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) _httpClient.getConnectionManager();
            int numConnections = cm.getConnectionsInPool();
            cm.closeIdleConnections(0, TimeUnit.MILLISECONDS);
            LOGGER.error(String.format(
                    "Got ConnectionPoolTimeoutException: %d connections before, %d after idle close",
                    numConnections, cm.getConnectionsInPool()));
        }

        throw new IOFetchException(url, e);
    } catch (URISyntaxException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (IllegalStateException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (BaseFetchException e) {
        throw e;
    } catch (Exception e) {
        // Map anything else to a generic IOFetchException
        // TODO KKr - create generic fetch exception
        throw new IOFetchException(url, new IOException(e));
    } finally {
        safeAbort(needAbort, request);
    }

    // Figure out how much data we want to try to fetch.
    int targetLength = _fetcherPolicy.getMaxContentSize();
    boolean truncated = false;
    String contentLengthStr = headerMap.getFirst(HttpHeaderNames.CONTENT_LENGTH);
    if (contentLengthStr != null) {
        try {
            int contentLength = Integer.parseInt(contentLengthStr);
            if (contentLength > targetLength) {
                truncated = true;
            } else {
                targetLength = contentLength;
            }
        } catch (NumberFormatException e) {
            // Ignore (and log) invalid content length values.
            LOGGER.warn("Invalid content length in header: " + contentLengthStr);
        }
    }

    // Now finally read in response body, up to targetLength bytes.
    // Note that entity might be null, for zero length responses.
    byte[] content = new byte[0];
    long readRate = 0;
    HttpEntity entity = response.getEntity();
    needAbort = true;

    if (entity != null) {
        InputStream in = null;

        try {
            in = entity.getContent();
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = 0;
            int totalRead = 0;
            ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BYTEARRAY_SIZE);

            int readRequests = 0;
            int minResponseRate = _fetcherPolicy.getMinResponseRate();
            // TODO KKr - we need to monitor the rate while reading a
            // single block. Look at HttpClient
            // metrics support for how to do this. Once we fix this, fix
            // the test to read a smaller (< 20K)
            // chuck of data.
            while ((totalRead < targetLength) && ((bytesRead = in.read(buffer, 0,
                    Math.min(buffer.length, targetLength - totalRead))) != -1)) {
                readRequests += 1;
                totalRead += bytesRead;
                out.write(buffer, 0, bytesRead);

                // Assume read time is at least one millisecond, to avoid DBZ
                // exception.
                long totalReadTime = Math.max(1, System.currentTimeMillis() - readStartTime);
                readRate = (totalRead * 1000L) / totalReadTime;

                // Don't bail on the first read cycle, as we can get a hiccup starting
                // out.
                // Also don't bail if we've read everything we need.
                if ((readRequests > 1) && (totalRead < targetLength) && (readRate < minResponseRate)) {
                    throw new AbortedFetchException(url, "Slow response rate of " + readRate + " bytes/sec",
                            AbortedFetchReason.SLOW_RESPONSE_RATE);
                }

                // Check to see if we got interrupted.
                if (Thread.interrupted()) {
                    throw new AbortedFetchException(url, AbortedFetchReason.INTERRUPTED);
                }
            }

            content = out.toByteArray();
            needAbort = truncated || (in.available() > 0);
        } catch (IOException e) {
            // We don't need to abort if there's an IOException
            throw new IOFetchException(url, e);
        } finally {
            safeAbort(needAbort, request);
            safeClose(in);
        }
    }

    return new FetchedResult(url, redirectedUrl, System.currentTimeMillis(), headerMap, content, contentType,
            (int) readRate, newBaseUrl, numRedirects, hostAddress);
}

From source file:com.naryx.tagfusion.cfm.http.cfHttpConnection.java

private void addCookies() {

    Map<String, List<String>> cookies = httpData.getCookies();
    Iterator<String> keys = cookies.keySet().iterator();
    String domain = "";
    domain = message.getURI().getHost();
    CookieStore cookieStore = new BasicCookieStore();

    HttpContext localContext = new BasicHttpContext();

    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

    while (keys.hasNext()) {
        String nextKey = keys.next();
        List<String> values = cookies.get(nextKey);
        Date date = new Date(2038, 1, 1);
        for (int i = 0; i < values.size(); i++) {
            BasicClientCookie cookie = new BasicClientCookie(nextKey, values.get(i));
            cookieStore.addCookie(cookie);
            cookie.setVersion(1);//from   w w  w.ja  v  a 2 s  .co m
            cookie.setDomain(domain);
            cookie.setPath("/");
            cookie.setExpiryDate(date);
            cookie.setSecure(false);
        }
    }
    client.setCookieStore(cookieStore);
}