Example usage for org.apache.http.client.methods HttpUriRequest addHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest addHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest addHeader.

Prototype

void addHeader(String str, String str2);

Source Link

Usage

From source file:org.wso2.am.integration.tests.other.APIMANAGER3965TestCase.java

@Test(groups = {
        "wso2.am" }, description = "Sample API creation", dependsOnMethods = "testAPICreationWithOutCorsConfiguration")
public void testAPICreationWithCorsConfiguration() throws Exception {
    JSONObject corsConfiguration = new JSONObject("{\"corsConfigurationEnabled\" : true, "
            + "\"accessControlAllowOrigins\" : [\"https://localhost:9443," + "http://localhost:8080\"], "
            + "\"accessControlAllowCredentials\" : true, " + "\"accessControlAllowHeaders\" : "
            + "[\"Access-Control-Allow-Origin\", \"authorization\", " + "\"Content-Type\", \"SOAPAction\"], "
            + "\"accessControlAllowMethods\" : [\"POST\", "
            + "\"PATCH\", \"GET\", \"DELETE\", \"OPTIONS\", \"PUT\"]}");
    apiRequest.setCorsConfiguration(corsConfiguration);
    apiRequest.setProvider(user.getUserName());
    apiPublisher.updateAPI(apiRequest);//from   w ww.  j  a v  a 2  s.  c om
    waitForAPIDeployment();
    waitForAPIDeploymentSync(apiRequest.getProvider(), apiRequest.getName(), apiRequest.getVersion(),
            APIMIntegrationConstants.IS_API_EXISTS);
    String apiInvocationUrl = getAPIInvocationURLHttp(apiContext + "/1.0.0/customers/123");
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpUriRequest option = new HttpOptions(apiInvocationUrl);
    option.addHeader("Origin", "http://localhost:8080");
    HttpResponse serviceResponse = httpclient.execute(option);
    String accessControlAllowOrigin = serviceResponse.getFirstHeader("Access-Control-Allow-Origin").getValue();
    String accessControlAllowHeaders = serviceResponse.getFirstHeader("Access-Control-Allow-Headers")
            .getValue();
    String accessControlAllowMethods = serviceResponse.getFirstHeader("Access-Control-Allow-Methods")
            .getValue();
    String accessControlAllowCredentials = serviceResponse.getFirstHeader("Access-Control-Allow-Credentials")
            .getValue();

    assertEquals(serviceResponse.getStatusLine().getStatusCode(), Response.Status.OK.getStatusCode(),
            "Response code mismatched when api invocation");
    assertEquals(accessControlAllowOrigin, "http://localhost:8080",
            "Access Control allow origin values get mismatched in option " + "Call");
    assertEquals(accessControlAllowHeaders, "Access-Control-Allow-Origin,authorization,Content-Type,SOAPAction",
            "Access Control allow Headers values get mismatched in option Call");
    assertTrue(accessControlAllowMethods.contains("GET") && !accessControlAllowMethods.contains("POST")
            && !accessControlAllowMethods.contains("DELETE") && !accessControlAllowMethods.contains("PUT")
            && !accessControlAllowMethods.contains("PATCH"),
            "Access Control allow Method values get mismatched in option Call");
    assertEquals(accessControlAllowCredentials, "true",
            "Access Control allow Credentials values get mismatched in option Call");
}

From source file:org.callimachusproject.rewrite.ProxyGetAdvice.java

protected Fluid service(String location, Header[] headers, ObjectMessage message, FluidBuilder fb)
        throws IOException, FluidException, ResponseException, OpenRDFException {
    String[] returnMedia = getReturnMedia();
    if (location == null)
        return fb.media(returnMedia);
    HttpUriRequest req = createRequest(location, headers, message, fb);
    if (returnMedia.length > 0) {
        for (String media : returnMedia) {
            req.addHeader("Accept", media);
        }//from   w w  w  . j  av  a 2  s  . c  om
        req.addHeader("Accept", "*/*;q=0.1");
    }
    assert message.getTarget() instanceof CalliObject;
    CalliRepository repository = ((CalliObject) message.getTarget()).getCalliRepository();
    HttpUriResponse resp = repository.getHttpClient(getSystemId()).getResponse(req);
    String systemId = resp.getSystemId();
    String contentType = "*/*";
    InputStream content = null;
    if (resp.getEntity() != null) {
        content = resp.getEntity().getContent();
    }
    if (resp.getFirstHeader("Content-Type") != null) {
        contentType = resp.getFirstHeader("Content-Type").getValue();
    }
    return fb.stream(content, systemId, contentType);
}

From source file:org.opennms.features.poller.remote.gwt.server.geocoding.MapquestGeocoder.java

/** {@inheritDoc} */
@Override/*from www  .j a v  a  2  s  . c  o  m*/
public GWTLatLng geocode(final String geolocation) throws GeocoderException {
    final HttpUriRequest method = new HttpGet(getUrl(geolocation));
    method.addHeader("User-Agent", "OpenNMS-MapQuestGeocoder/1.0");
    method.addHeader("Referer", m_referer);

    try {
        InputStream responseStream = m_httpClient.execute(method).getEntity().getContent();
        final ElementTree tree = ElementTree.fromStream(responseStream);
        if (tree == null) {
            throw new GeocoderException(
                    "an error occurred connecting to the MapQuest geocoding service (no XML tree was found)");
        }

        final ElementTree statusCode = tree.find("//statusCode");
        if (statusCode == null || !statusCode.getText().equals("0")) {
            final String code = (statusCode == null ? "unknown" : statusCode.getText());
            final ElementTree messageTree = tree.find("//message");
            final String message = (messageTree == null ? "unknown" : messageTree.getText());
            throw new GeocoderException("an error occurred when querying MapQuest (statusCode=" + code
                    + ", message=" + message + ")");
        }

        final List<ElementTree> locations = tree.findAll("//location");
        if (locations.size() > 1) {
            LOG.warn("more than one location returned for query: {}", geolocation);
        } else if (locations.size() == 0) {
            throw new GeocoderException("MapQuest returned an OK status code, but no locations");
        }
        final ElementTree location = locations.get(0);

        // first, check the quality
        if (m_minimumQuality != null) {
            final Quality geocodeQuality = Quality
                    .valueOf(location.find("//geocodeQuality").getText().toUpperCase());
            if (geocodeQuality.compareTo(m_minimumQuality) < 0) {
                throw new GeocoderException("response did not meet minimum quality requirement ("
                        + geocodeQuality + " is less specific than " + m_minimumQuality + ")");
            }
        }

        // then, extract the lat/lng
        final ElementTree latLng = location.find("//latLng");
        Double latitude = Double.valueOf(latLng.find("//lat").getText());
        Double longitude = Double.valueOf(latLng.find("//lng").getText());
        return new GWTLatLng(latitude, longitude);
    } catch (GeocoderException e) {
        throw e;
    } catch (Throwable e) {
        throw new GeocoderException("unable to get lat/lng from MapQuest", e);
    }
}

From source file:cn.com.loopj.android.http.SyncHttpClient.java

@Override
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {/*from   www .j  av a 2s  .c o  m*/
    if (contentType != null) {
        uriRequest.addHeader(AsyncHttpClient.HEADER_CONTENT_TYPE, contentType);
    }

    responseHandler.setUseSynchronousMode(true);

    /*
       * will execute the request directly
    */
    newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context).run();

    // Return a Request Handle that cannot be used to cancel the request
    // because it is already complete by the time this returns
    return new RequestHandle(null);
}

From source file:com.fdwills.external.http.SyncHttpClient.java

@Override
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {/*from  w w  w . j a  v  a  2s .  c  o  m*/
    if (contentType != null) {
        uriRequest.addHeader(AsyncHttpClient.HEADER_CONTENT_TYPE, contentType);
    }

    responseHandler.setUseSynchronousMode(true);

    /*
     * will execute the request directly
    */
    newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context).run();

    // Return a Request Handle that cannot be used to cancel the request
    // because it is already complete by the time this returns
    return new RequestHandle(null);
}

From source file:com.iflytek.iFramework.http.asynhttp.SyncHttpClient.java

@Override
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {//from   w  ww .  j  av  a  2s.  c  om
    if (contentType != null) {
        uriRequest.addHeader(HEADER_CONTENT_TYPE, contentType);
    }

    responseHandler.setUseSynchronousMode(true);

    /*
       * will execute the request directly
    */
    newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context).run();

    // Return a Request Handle that cannot be used to cancel the request
    // because it is already complete by the time this returns
    return new RequestHandle(null);
}

From source file:org.hmzb.test.HttpClientTest.java

public void testJianShen() throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpUriRequest request = new HttpGet("http://10.5.17.74/whatEvents.aspx/T-154");
    request.addHeader("Cookie",
            "ASP.NET_SessionId=3omex455vqahphzrr44qhu45; SBVerifyCode=DZI9pWG9uNo47bay6TyPkS8U8P0=; SBVerifyCode_CurrentLevel=Normal; .SPBForms=1AD1E7DF6CB3E81313AB04B11B024BF2E1816D8E18EC76D1EB4A6DEE9D2542377E5AD00F8AD42D1DA80D6667F1CE9E9F361C7B5DC4D1876A7DAB2A24AEF04DD95A191AE473010351");
    // HttpResponse response = client.execute(request);
    String result = (String) HttpClientUtil.getResult(client, request).get("result");
    System.out.println(result);/*from  w  w w.  j a v a  2s .  c o  m*/
    if (result.contains("??")) {
        System.out.println("?");
    }
}

From source file:com.ab.http.SyncHttpClient.java

/**
 * ??TODO//from  w  w  w .  j a  v  a  2 s .  c  o  m
 * @see com.ab.http.AsyncHttpClient#sendRequest(org.apache.http.impl.client.DefaultHttpClient, org.apache.http.protocol.HttpContext, org.apache.http.client.methods.HttpUriRequest, java.lang.String, com.ab.http.AsyncHttpResponseHandler, android.content.Context)
 * @author: zhaoqp
 * @date2013-10-22 ?4:23:15
 * @version v1.0
 */
@Override
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, AsyncHttpResponseHandler responseHandler,
        Context context) {
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }

    responseHandler.setUseSynchronousMode(true);

    /*
       * will execute the request directly
    */
    new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler).run();

    // Return a Request Handle that cannot be used to cancel the request
    // because it is already complete by the time this returns
    return new RequestHandle(null);
}

From source file:com.amytech.android.library.utils.asynchttp.SyncHttpClient.java

@Override
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {/*from  w  ww. ja  v  a 2 s.co  m*/
    if (contentType != null) {
        uriRequest.addHeader(AsyncHttpClient.HEADER_CONTENT_TYPE, contentType);
    }

    responseHandler.setUseSynchronousMode(true);

    /*
     * will execute the request directly
     */
    newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context).run();

    // Return a Request Handle that cannot be used to cancel the request
    // because it is already complete by the time this returns
    return new RequestHandle(null);
}

From source file:com.alading.library.TASyncHttpClient.java

@Override
protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest,
        String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }/*  w  w  w .j  ava 2  s .c o  m*/
    new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler).run();
}