Example usage for org.apache.commons.httpclient HttpMethod addRequestHeader

List of usage examples for org.apache.commons.httpclient HttpMethod addRequestHeader

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod addRequestHeader.

Prototype

public abstract void addRequestHeader(String paramString1, String paramString2);

Source Link

Usage

From source file:org.archive.crawler.fetcher.FetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 *///from   w  ww  .  ja  va 2  s  . com
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()
            && (curi.getViaContext() == null || !Link.PREREQ_MISC.equals(curi.getViaContext().toString()))) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    if (!curi.isPrerequisite()) {
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE,
                CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, CoreAttributeConstants.A_ETAG_HEADER,
                "If-None-Match");
    }

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 *///from   www.  j a v a 2  s.c  o  m
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()
            && (curi.getViaContext() == null || !Link.PREREQ_MISC.equals(curi.getViaContext().toString()))) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    /*if(!curi.isPrerequisite() && curi.containsKey(URLInfo.MODIFY_TIME) &&
       (Boolean)getUncheckedAttribute(curi, ATTR_SEND_IF_MODIFIED_SINCE)) {
      long modifyTime = curi.getLong(URLInfo.MODIFY_TIME);
      if (modifyTime != 0) {
      Date date = new Date(modifyTime);
      method.setRequestHeader("If-Modified-Since", date.toString());
      logger.debug(curi.getUURI().toString() + " send header modifyTime:" + date.toGMTString());
      }
              
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE, 
           CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, 
        CoreAttributeConstants.A_ETAG_HEADER, "If-None-Match");
    }*/

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HttpClient http = getClient();
    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:org.archive.wayback.resourceindex.ziplines.Http11BlockLoader.java

/**
 * Fetch a range of bytes from a particular URL. Note that the bytes are
 * read into memory all at once, so care should be taken with the length
 * argument./*  w  w  w .  j a  v a  2s .  com*/
 * 
 * @param url String URL to fetch
 * @param offset byte start offset of the desired range
 * @param length number of octets to fetch
 * @return a new byte[] containing the octets fetched
 * @throws IOException on HTTP and Socket failures, as well as Timeouts
 */
public byte[] getBlock(String url, long offset, int length) throws IOException {

    HttpMethod method = null;
    try {
        method = new GetMethod(url);
    } catch (IllegalArgumentException e) {
        LOGGER.warning("Bad URL for block fetch:" + url);
        throw new IOException("Url:" + url + " does not look like an URL?");
    }
    StringBuilder sb = new StringBuilder(16);
    sb.append(ZiplinedBlock.BYTES_HEADER).append(offset);
    sb.append(ZiplinedBlock.BYTES_MINUS).append((offset + length) - 1);
    String rangeHeader = sb.toString();
    method.addRequestHeader(ZiplinedBlock.RANGE_HEADER, rangeHeader);
    //uc.setRequestProperty(RANGE_HEADER, sb.toString());
    long start = System.currentTimeMillis();
    try {
        LOGGER.fine("Reading block:" + url + "(" + rangeHeader + ")");
        int status = http.executeMethod(method);
        if ((status == 200) || (status == 206)) {
            InputStream is = method.getResponseBodyAsStream();
            byte[] block = new byte[length];
            ByteStreams.readFully(is, block);
            long elapsed = System.currentTimeMillis() - start;
            PerformanceLogger.noteElapsed("CDXBlockLoad", elapsed, url);
            return block;

        } else {
            throw new IOException("Bad status for " + url);
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:org.codehaus.wadi.web.impl.CommonsHttpProxy.java

protected void doProxy(URI uri, WebInvocation context) throws ProxyingException {
    HttpServletRequest hreq = context.getHreq();
    HttpServletResponse hres = context.getHres();

    long startTime = System.currentTimeMillis();

    String m = hreq.getMethod();/* w  w w. j  a  v a2  s  . c  o m*/
    Class clazz = (Class) _methods.get(m);
    if (clazz == null) {
        throw new IrrecoverableException("unsupported http method: " + m);
    }

    HttpMethod hm = null;
    try {
        hm = (HttpMethod) clazz.newInstance();
    } catch (Exception e) {
        throw new IrrecoverableException("could not create HttpMethod instance", e); // should never happen
    }

    String requestURI = getRequestURI(hreq);
    hm.setPath(requestURI);

    String queryString = hreq.getQueryString();
    if (queryString != null) {
        hm.setQueryString(queryString);
        requestURI += queryString;
    }

    hm.setFollowRedirects(false);
    //hm.setURI(new URI(uri));
    hm.setStrictMode(false);

    // check connection header
    String connectionHdr = hreq.getHeader("Connection"); // TODO - what if there are multiple values ?
    if (connectionHdr != null) {
        connectionHdr = connectionHdr.toLowerCase();
        if (connectionHdr.equals("keep-alive") || connectionHdr.equals("close"))
            connectionHdr = null; // TODO  ??
    }

    // copy headers
    boolean xForwardedFor = false;
    boolean hasContent = false;
    int contentLength = 0;
    Enumeration enm = hreq.getHeaderNames();
    while (enm.hasMoreElements()) {
        // TODO could be better than this! - using javax.servlet ?
        String hdr = (String) enm.nextElement();
        String lhdr = hdr.toLowerCase();

        if (_DontProxyHeaders.contains(lhdr))
            continue;
        if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0)
            continue;

        if ("content-length".equals(lhdr)) {
            try {
                contentLength = hreq.getIntHeader(hdr);
                hasContent = contentLength > 0;
            } catch (NumberFormatException e) {
                if (_log.isWarnEnabled())
                    _log.warn("bad Content-Length header value: " + hreq.getHeader(hdr), e);
            }
        }

        if ("content-type".equals(lhdr)) {
            hasContent = true;
        }

        Enumeration vals = hreq.getHeaders(hdr);
        while (vals.hasMoreElements()) {
            String val = (String) vals.nextElement();
            if (val != null) {
                hm.addRequestHeader(hdr, val);
                // if (_log.isInfoEnabled()) _log.info("Request " + hdr + ": " + val);
                xForwardedFor |= "X-Forwarded-For".equalsIgnoreCase(hdr); // why is this not in the outer loop ?
            }
        }
    }

    // cookies...

    // although we copy cookie headers into the request abover - commons-httpclient thinks it knows better and strips them out before sending.
    // we have to explicitly use their interface to add the cookies - painful...

    // DOH! - an org.apache.commons.httpclient.Cookie is NOT a
    // javax.servlet.http.Cookie - and it looks like the two don't
    // map onto each other without data loss...
    HttpState state = new HttpState();
    javax.servlet.http.Cookie[] cookies = hreq.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            javax.servlet.http.Cookie c = cookies[i];
            String domain = c.getDomain();
            if (domain == null) {
                domain = hreq.getServerName(); // TODO - tmp test
                // _log.warn("defaulting cookie domain");
            }
            //     domain=null;
            String cpath = c.getPath();
            if (cpath == null) {
                cpath = hreq.getContextPath(); // fix for Jetty
                // _log.warn("defaulting cookie path");
            }
            //if (_log.isTraceEnabled()) _log.trace("PATH: value="+path+" length="+(path==null?0:path.length()));
            Cookie cookie = new Cookie(domain, c.getName(), c.getValue(), cpath, c.getMaxAge(), c.getSecure()); // TODO - sort out domain
            //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.getDomain()+","+ cookie.getName()+","+ cookie.getValue()+","+ cookie.getPath()+","+ cookie.getExpiryDate()+","+ cookie.getSecure());
            state.addCookie(cookie);
            //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.toString());
        }
    }

    // Proxy headers
    hm.addRequestHeader("Via", "1.1 " + hreq.getLocalName() + ":" + hreq.getLocalPort() + " \"WADI\"");
    if (!xForwardedFor)
        hm.addRequestHeader("X-Forwarded-For", hreq.getRemoteAddr());
    // Max-Forwards...

    // a little bit of cache control
    //      String cache_control = hreq.getHeader("Cache-Control");
    //      if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0))
    //      httpMethod.setUseCaches(false);

    // customize Connection
    //      uc.setDoInput(true);

    int client2ServerTotal = 0;
    if (hasContent) {
        //         uc.setDoOutput(true);

        try {
            if (hm instanceof EntityEnclosingMethod)
                ((EntityEnclosingMethod) hm).setRequestBody(hreq.getInputStream());
            // TODO - do we need to close response stream at end... ?
        } catch (IOException e) {
            throw new IrrecoverableException("could not pss request input across proxy", e);
        }
    }

    try {
        HttpClient client = new HttpClient();
        HostConfiguration hc = new HostConfiguration();
        //String host=location.getAddress().getHostAddress();
        // inefficient - but stops httpclient from rejecting half our cookies...
        String host = uri.getHost();
        hc.setHost(host, uri.getPort());
        client.executeMethod(hc, hm, state);
    } catch (IOException e) // TODO
    {
        _log.warn("problem proxying connection:", e);
    }

    InputStream fromServer = null;

    // handler status codes etc.
    int code = 502;
    //      String message="Bad Gateway: could not read server response code or message";

    code = hm.getStatusCode(); // IOException
    //      message=hm.getStatusText(); // IOException
    hres.setStatus(code);
    //      hres.setStatus(code, message); - deprecated...

    try {
        fromServer = hm.getResponseBodyAsStream(); // IOException
    } catch (IOException e) {
        _log.warn("problem acquiring http client output", e);
    }

    // clear response defaults.
    hres.setHeader("Date", null);
    hres.setHeader("Server", null);

    // set response headers
    // TODO - is it a bug in Jetty that I have to start my loop at 1 ? or that key[0]==null ?
    // Try this inside Tomcat...
    Header[] headers = hm.getResponseHeaders();
    for (int i = 0; i < headers.length; i++) {
        String h = headers[i].toExternalForm();
        int index = h.indexOf(':');
        String key = h.substring(0, index).trim().toLowerCase();
        String val = h.substring(index + 1, h.length()).trim();
        if (val != null && !_DontProxyHeaders.contains(key)) {
            hres.addHeader(key, val);
            // if (_log.isInfoEnabled()) _log.info("Response: "+key+" - "+val);
        }
    }

    hres.addHeader("Via", "1.1 (WADI)");

    // copy server->client
    int server2ClientTotal = 0;
    if (fromServer != null) {
        try {
            OutputStream toClient = hres.getOutputStream();// IOException
            server2ClientTotal += copy(fromServer, toClient, 8192);// IOException
        } catch (IOException e) {
            _log.warn("problem proxying server response back to client", e);
        } finally {
            try {
                fromServer.close();
            } catch (IOException e) {
                // well - we did our best...
                _log.warn("problem closing server response stream", e);
            }
        }
    }

    long endTime = System.currentTimeMillis();
    long elapsed = endTime - startTime;
    if (_log.isDebugEnabled()) {
        _log.debug("in:" + client2ServerTotal + ", out:" + server2ClientTotal + ", status:" + code + ", time:"
                + elapsed + ", uri:" + uri);
    }
}

From source file:org.cryptomator.frontend.webdav.WebDavServerTest.java

@Test
public void testGetWithUnsatisfiableRange() throws IOException {
    final HttpClient client = new HttpClient();

    // write test content:
    final byte[] testContent = "hello world".getBytes();
    try (WritableFile w = fs.file("foo.txt").openWritable()) {
        w.write(ByteBuffer.wrap(testContent));
    }/*  www.j  a va  2 s .c o  m*/

    // check get response body:
    final HttpMethod getMethod = new GetMethod(servletRoot + "/foo.txt");
    getMethod.addRequestHeader("Range", "chunks=1-2");
    final int statusCode = client.executeMethod(getMethod);
    Assert.assertEquals(200, statusCode);
    Assert.assertArrayEquals(testContent, getMethod.getResponseBody());
    getMethod.releaseConnection();
}

From source file:org.cryptomator.frontend.webdav.WebDavServerTest.java

@Test
public void testMultipleGetWithRangeAsync() throws IOException, URISyntaxException, InterruptedException {
    final String testResourceUrl = servletRoot + "/foo.txt";

    // prepare 8MiB test data:
    final byte[] plaintextData = new byte[2097152 * Integer.BYTES];
    final ByteBuffer plaintextDataByteBuffer = ByteBuffer.wrap(plaintextData);
    for (int i = 0; i < 2097152; i++) {
        plaintextDataByteBuffer.putInt(i);
    }//from w  ww . j ava 2 s.  c o m
    try (WritableFile w = fs.file("foo.txt").openWritable()) {
        plaintextDataByteBuffer.flip();
        w.write(plaintextDataByteBuffer);
    }

    final MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
    cm.getParams().setDefaultMaxConnectionsPerHost(50);
    final HttpClient client = new HttpClient(cm);

    // multiple async range requests:
    final List<ForkJoinTask<?>> tasks = new ArrayList<>();
    final Random generator = new Random(System.currentTimeMillis());

    final AtomicBoolean success = new AtomicBoolean(true);

    // 10 full interrupted requests:
    for (int i = 0; i < 10; i++) {
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                final int statusCode = client.executeMethod(getMethod);
                if (statusCode != 200) {
                    LOG.error("Invalid status code for interrupted full request");
                    success.set(false);
                }
                getMethod.getResponseBodyAsStream().read();
                getMethod.getResponseBodyAsStream().close();
                getMethod.releaseConnection();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 50 crappy interrupted range requests:
    for (int i = 0; i < 50; i++) {
        final int lower = generator.nextInt(plaintextData.length);
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-");
                final int statusCode = client.executeMethod(getMethod);
                if (statusCode != 206) {
                    LOG.error("Invalid status code for interrupted range request");
                    success.set(false);
                }
                getMethod.getResponseBodyAsStream().read();
                getMethod.getResponseBodyAsStream().close();
                getMethod.releaseConnection();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 50 normal open range requests:
    for (int i = 0; i < 50; i++) {
        final int lower = generator.nextInt(plaintextData.length - 512);
        final int upper = plaintextData.length - 1;
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-");
                final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1);
                final int statusCode = client.executeMethod(getMethod);
                final byte[] responseBody = new byte[upper - lower + 10];
                final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody);
                getMethod.releaseConnection();
                if (statusCode != 206) {
                    LOG.error("Invalid status code for open range request");
                    success.set(false);
                } else if (upper - lower + 1 != bytesRead) {
                    LOG.error("Invalid response length for open range request");
                    success.set(false);
                } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) {
                    LOG.error("Invalid response body for open range request");
                    success.set(false);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 200 normal closed range requests:
    for (int i = 0; i < 200; i++) {
        final int pos1 = generator.nextInt(plaintextData.length - 512);
        final int pos2 = pos1 + 512;
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final int lower = Math.min(pos1, pos2);
                final int upper = Math.max(pos1, pos2);
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-" + upper);
                final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1);
                final int statusCode = client.executeMethod(getMethod);
                final byte[] responseBody = new byte[upper - lower + 1];
                final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody);
                getMethod.releaseConnection();
                if (statusCode != 206) {
                    LOG.error("Invalid status code for closed range request");
                    success.set(false);
                } else if (upper - lower + 1 != bytesRead) {
                    LOG.error("Invalid response length for closed range request");
                    success.set(false);
                } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) {
                    LOG.error("Invalid response body for closed range request");
                    success.set(false);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    Collections.shuffle(tasks, generator);

    final ForkJoinPool pool = new ForkJoinPool(4);
    for (ForkJoinTask<?> task : tasks) {
        pool.execute(task);
    }
    for (ForkJoinTask<?> task : tasks) {
        task.join();
    }
    pool.shutdown();
    cm.shutdown();

    Assert.assertTrue(success.get());
}

From source file:org.deegree.ogcwebservices.wass.wss.operation.DoServiceHandler.java

/**
 * This method does the actual request to the secured service. It returns the response of the
 * secured service as an inputstream. It also replace the GetCapabilities request - responses
 * with the facadeurl given by the client.
 *
 * @param request/* w w w .java 2s.  c o m*/
 *            send by the client a DoService Request.
 * @param securedService
 *            the service for which this wss is proxying, must be put in the deegreeparams of
 *            the configuration file.
 * @param requestedCharset
 *            this wss uses, also read from the deegreeparams in the configuration file.
 * @param timeout
 *            how long to wait for a response. Service dependable therefor also read from the
 *            deegreeparams in the config file.
 * @param securedServiceName
 *            the name of the service for which we are proxying -> config.
 * @return the http response of the secured service as an inputstream.
 * @throws DoServiceException
 *             if an error occurs wile sending the request or treating the response. see
 *             org.deegree.ogcwebservices.csw.manager.CatalogueHarvester#getNextMetadataRecord
 */
public DoServiceResponse sendRequest(DoService request, URL securedService, String requestedCharset,
        int timeout, String securedServiceName) throws DoServiceException {
    if (requestAllowed) {

        Header[] headers = null;
        InputStream body = null;
        Header[] footers = null;
        String proxyRequest = null;
        try {
            proxyRequest = URLDecoder.decode(request.getPayload(), CharsetUtils.getSystemCharset());
        } catch (UnsupportedEncodingException e) {
            LOG.logError(e.getMessage(), e);
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_INTERNAL", "WSS"));
        }
        LOG.logDebug("encoded proxyrequest: " + request.getPayload() + "\ndecoded proxy: " + proxyRequest);
        String dcp = request.getDcp();
        HttpClient client = new HttpClient();
        client = WebUtils.enableProxyUsage(client, securedService);
        StringRequestEntity requestEntity = null;
        HttpClientParams params = client.getParams();
        params.setSoTimeout(timeout);
        HttpMethod requestMethod = null;
        try {
            String contentType = null;
            for (RequestParameter param : request.getRequestParameters()) {
                if (param.getId().toLowerCase().trim().contains("mime-type"))
                    contentType = param.getParameter();
            }
            requestEntity = new StringRequestEntity(proxyRequest, contentType, requestedCharset);
        } catch (UnsupportedEncodingException e1) {
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_ENCODING_NOT_SUPPORTED", "WSS"));
        }
        if (dcp.equalsIgnoreCase("http_post")) {

            // the url to the service must be written in the deegreeparams in the configuration
            // xml
            requestMethod = new PostMethod(securedService.toExternalForm());
            ((PostMethod) requestMethod).setRequestEntity(requestEntity);
        } else if (dcp.equalsIgnoreCase("http_get")) {
            requestMethod = new GetMethod(securedService.toExternalForm());
            requestMethod.setQueryString(proxyRequest);
        } else {
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_NOT_POST_OR_GET", "WSS"));
        }
        // getDataRequest
        try {
            // make header parameters of the requestParameters.
            for (RequestParameter param : request.getRequestParameters()) {
                if (!param.getId().toLowerCase().trim().contains("mime-type"))// Contenttype
                    requestMethod.addRequestHeader(param.getId(), param.getParameter());
            }
            // Call the secured service
            client.executeMethod(requestMethod);
            headers = requestMethod.getResponseHeaders();
            footers = requestMethod.getResponseFooters();
            body = requestMethod.getResponseBodyAsStream();

            if (body == null)
                throw new DoServiceException(Messages.getMessage("WASS_ERROR_GOT_NO_RESPONSE", "WSS"));
        } catch (HttpException e) {
            LOG.logError(e.getMessage(), e);
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_EXCEPTION_IN_RESPONSE", "WSS"));
        } catch (IOException e) {
            LOG.logError(e.getMessage(), e);
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_IN_TRANSPORT", "WSS"));
        }
        try {
            // Replace the given urls with the facadeurls if it is a GetCapabilities request
            if (proxyRequest.trim().contains("GetCapabilities")) {
                Operation[] operations = null;
                OGCCapabilitiesDocument doc = null;
                /*
                 * For now just check these service, others may be "secured" in the future.
                 */
                if ("WFS".equals(securedServiceName)) {
                    doc = new WFSCapabilitiesDocument();
                    doc.load(body, securedService.toExternalForm());
                    WFSCapabilities cap = (WFSCapabilities) doc.parseCapabilities();
                    operations = cap.getOperationsMetadata().getOperations();
                    replaceFacadeURL(operations, request.getFacadeURL());
                    doc = org.deegree.ogcwebservices.wfs.XMLFactory.export(cap);
                } else if (("WMS").equals(securedServiceName)) {
                    doc = new WMSCapabilitiesDocument();
                    doc.load(body, securedService.toExternalForm());
                    doc = WMSCapabilitiesDocumentFactory.getWMSCapabilitiesDocument(doc.getRootElement());
                    WMSCapabilities cap = (WMSCapabilities) doc.parseCapabilities();
                    org.deegree.owscommon_new.Operation[] ops = cap.getOperationMetadata().getOperations()
                            .toArray(new org.deegree.owscommon_new.Operation[0]);
                    replaceFacadeURL(ops, request.getFacadeURL());
                    doc = org.deegree.ogcwebservices.wms.XMLFactory.export(cap);
                } else if (("WCS").equals(securedServiceName)) {
                    doc = new WCSCapabilitiesDocument();
                    doc.load(body, securedService.toExternalForm());
                    WCSCapabilities cap = (WCSCapabilities) doc.parseCapabilities();
                    operations = cap.getCapabilitiy().getOperations().getOperations();
                    replaceFacadeURL(operations, request.getFacadeURL());
                    doc = org.deegree.ogcwebservices.wcs.XMLFactory.export(cap);
                } else if (("CSW").equals(securedServiceName)) {
                    doc = new CatalogueCapabilitiesDocument();
                    doc.load(body, securedService.toExternalForm());
                    CatalogueCapabilities cap = (CatalogueCapabilities) doc.parseCapabilities();
                    operations = cap.getOperationsMetadata().getOperations();
                    replaceFacadeURL(operations, request.getFacadeURL());
                    doc = org.deegree.ogcwebservices.csw.XMLFactory_2_0_0.export(cap, null);
                }

                body = new ByteArrayInputStream(doc.getAsString().getBytes());
            }
        } catch (IOException e) {
            LOG.logError(e.getMessage(), e);
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_READING_BODY", "WSS"));
        } catch (InvalidCapabilitiesException e) {
            LOG.logError(e.getMessage(), e);
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_CAPABILITIES_RESPONSE", "WSS"));
        } catch (SAXException e) {
            LOG.logError(e.getMessage(), e);
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_FACADE_URL", "WSS"));
        } catch (XMLParsingException e) {
            LOG.logError(e.getMessage(), e);
            throw new DoServiceException(Messages.getMessage("WASS_ERROR_READING_BODY", "WSS"));
        }
        return new DoServiceResponse(headers, body, footers);
    }

    return null;
}

From source file:org.deri.pipes.utils.HttpResponseCache.java

/**
 * @param headMethod//from www .ja  va2 s  .c o  m
 * @param requestHeaders
 */
private static void addRequestHeaders(HttpMethod method, Map<String, String> requestHeaders) {
    if (requestHeaders != null) {
        for (String key : requestHeaders.keySet()) {
            method.addRequestHeader(key, requestHeaders.get(key));
        }
    }
}

From source file:org.eclipse.e4.opensocial.container.internal.browserHandlers.core.MakeXmlHttpRequestHandler.java

/**
 * Parse the String corresponding the headers and add them to the HttpMethod
 * // www.  j  a va2  s .  c  o  m
 * @param httpMethod
 *            HttpMethod which will be filled with the given headers
 * @param headers
 *            String representation of the headers to add. They respect the
 *            following format :
 *            headerName#headerValue>header2Name#header2Value
 * 
 */
private void addHeaders(HttpMethod httpMethod, String headers) {
    // Split to retrieve all headers
    String[] splittedHeader = headers.split(HEADERS_SEPARATOR);

    // Loop through the headers
    for (String header : splittedHeader) {
        // Split to retrieve the header name & value
        String[] headerNameAndValue = header.split(HEADER_NAME_VALUE_SEPARATOR);
        String name = headerNameAndValue[0];
        String value = headerNameAndValue[1];
        // If the value exists add it to the
        // HttpMethod
        if (!NO_HEADER_VALUE.equals(value))
            httpMethod.addRequestHeader(name, value);
    }
}

From source file:org.eclipse.e4.ui.internal.gadgets.opensocial.browserfunctions.MakeXmlHttpRequest.java

/**
 * Parse the String corresponding the headers and add them to the HttpMethod
 * // w  ww . ja  v a 2 s  . c  o m
 * @param httpMethod
 *            HttpMethod which will be filled with the given headers
 * @param headers
 *            String representation of the headers to add. They respect the
 *            following format :
 *            headerName#headerValue>header2Name#header2Value
 * 
 */
private void addHeaders(HttpMethod httpMethod, String headers) {
    // Split to retrieve all headers
    String[] splittedHeader = headers.split(OpenSocialView.HEADERS_SEPARATOR);

    // Loop through the headers
    for (String header : splittedHeader) {
        // Split to retrieve the header name & value
        String[] headerNameAndValue = header.split(OpenSocialView.HEADER_NAME_VALUE_SEPARATOR);
        String name = headerNameAndValue[0];
        String value = headerNameAndValue[1];
        // If the value exists add it to the
        // HttpMethod
        if (!OpenSocialView.NO_HEADER_VALUE.equals(value))
            httpMethod.addRequestHeader(name, value);
    }
}