Example usage for org.apache.http.client.methods HttpOptions HttpOptions

List of usage examples for org.apache.http.client.methods HttpOptions HttpOptions

Introduction

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

Prototype

public HttpOptions(final String uri) 

Source Link

Usage

From source file:org.expath.httpclient.impl.ApacheHttpConnection.java

public void setRequestMethod(String method, boolean with_content) throws HttpClientException {
    if (LOG.isInfoEnabled()) {
        LOG.debug("Request method: " + method + " (" + with_content + ")");
    }/*from   w  ww. ja  va  2s  .c om*/
    String uri = myUri.toString();
    String m = method.toUpperCase();
    if ("DELETE".equals(m)) {
        myRequest = new HttpDelete(uri);
    } else if ("GET".equals(m)) {
        myRequest = new HttpGet(uri);
    } else if ("HEAD".equals(m)) {
        myRequest = new HttpHead(uri);
    } else if ("OPTIONS".equals(m)) {
        myRequest = new HttpOptions(uri);
    } else if ("POST".equals(m)) {
        myRequest = new HttpPost(uri);
    } else if ("PUT".equals(m)) {
        myRequest = new HttpPut(uri);
    } else if ("TRACE".equals(m)) {
        myRequest = new HttpTrace(uri);
    } else if (!checkMethodName(method)) {
        throw new HttpClientException("Invalid HTTP method name [" + method + "]");
    } else if (with_content) {
        myRequest = new AnyEntityMethod(m, uri);
    } else {
        myRequest = new AnyEmptyMethod(m, uri);
    }
}

From source file:com.supremainc.biostar2.sdk.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///from   www. ja va 2  s .  com
@SuppressWarnings("deprecation")
/* protected */static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for
        // backwards compatibility.
        // If the request's post body is null, then the assumption is
        // that the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:TestHTTPSource.java

@Test
public void testOptions() throws Exception {
    doTestForbidden(new HttpOptions("http://0.0.0.0:" + selectedPort));
}

From source file:org.apache.openmeetings.service.calendar.caldav.AppointmentManager.java

/**
 * Tests if the Calendar's URL can be accessed, or not.
 *
 * @param client   Client which makes the connection.
 * @param context http context/*from  w  w w . j a  va  2 s . c  o m*/
 * @param calendar Calendar whose URL is to be accessed.
 * @return Returns true for HTTP Status 200, or 204, else false.
 */
public boolean testConnection(HttpClient client, HttpClientContext context, OmCalendar calendar) {
    cleanupIdleConnections();

    HttpOptions optionsMethod = null;
    try {
        String path = calendar.getHref();
        optionsMethod = new HttpOptions(path);
        optionsMethod.setHeader("Accept", "*/*");
        HttpResponse response = client.execute(optionsMethod, context);
        int status = response.getStatusLine().getStatusCode();
        if (status == SC_OK || status == SC_NO_CONTENT) {
            return true;
        }
    } catch (IOException e) {
        log.error("Error executing OptionsMethod during testConnection.", e);
    } catch (Exception e) {
        //Should not ever reach here.
        log.error("Severe Error in executing OptionsMethod during testConnection.", e);
    } finally {
        if (optionsMethod != null) {
            optionsMethod.reset();
        }
    }
    return false;
}

From source file:org.wisdom.test.http.HttpClientHelper.java

private static HttpRequestBase prepareRequest(HttpRequest request) {

    if (!request.getHeaders().containsKey(HeaderNames.USER_AGENT)) {
        request.header(HeaderNames.USER_AGENT, USER_AGENT);
    }/* w ww . j a v a 2 s.  c o m*/

    if (!request.getHeaders().containsKey(HeaderNames.ACCEPT_ENCODING)) {
        request.header(HeaderNames.ACCEPT_ENCODING, "gzip");
    }

    Object defaultHeaders = Options.getOption(Options.Option.DEFAULT_HEADERS);
    if (defaultHeaders != null) {
        @SuppressWarnings("unchecked")
        Set<Map.Entry<String, String>> entrySet = ((Map<String, String>) defaultHeaders).entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            request.header(entry.getKey(), entry.getValue());
        }
    }

    HttpRequestBase reqObj = null;

    switch (request.getHttpMethod()) {
    case GET:
        reqObj = new HttpGet(request.getUrl());
        break;
    case POST:
        reqObj = new HttpPost(request.getUrl());
        break;
    case PUT:
        reqObj = new HttpPut(request.getUrl());
        break;
    case DELETE:
        reqObj = new HttpDeleteWithBody(request.getUrl());
        break;
    case OPTIONS:
        reqObj = new HttpOptions(request.getUrl());
        break;
    default:
        break;
    }

    if (reqObj == null) {
        throw new IllegalStateException(
                "Cannot build the request - unsupported HTTP verb : " + request.getHttpMethod());
    }

    for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
        reqObj.addHeader(entry.getKey(), entry.getValue());
    }

    // Set body
    if (request.getHttpMethod() != HttpMethod.GET && request.getBody() != null
            && reqObj instanceof HttpEntityEnclosingRequestBase) {
        ((HttpEntityEnclosingRequestBase) reqObj).setEntity(request.getBody().getEntity());
    }

    return reqObj;
}

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  w w  .ja va2  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.rapidoid.http.HttpClientUtil.java

private static HttpRequestBase createReq(HttpReq config, String url) {
    HttpRequestBase req;//from w ww .  j  av a  2 s .  c o  m
    switch (config.verb()) {
    case GET:
        req = new HttpGet(url);
        break;

    case POST:
        req = new HttpPost(url);
        break;

    case PUT:
        req = new HttpPut(url);
        break;

    case DELETE:
        req = new HttpDelete(url);
        break;

    case PATCH:
        req = new HttpPatch(url);
        break;

    case OPTIONS:
        req = new HttpOptions(url);
        break;

    case HEAD:
        req = new HttpHead(url);
        break;

    case TRACE:
        req = new HttpTrace(url);
        break;

    default:
        throw Err.notExpected();
    }
    return req;
}

From source file:com.android.volley.toolbox.http.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///w  ww . j  av a2s  .  c  o m
@SuppressWarnings("deprecation")
/* protected */ HttpUriRequest createHttpRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws AuthFailureError, IOException {

    String url = request.getUrl();
    if (mUrlRewriter != null) {
        url = mUrlRewriter.rewriteUrl(url);
        if (url == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
    }
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(url);
            //                    postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(url);
        }
    }
    case Method.GET:
        return new HttpGet(url);
    case Method.DELETE:
        return new HttpDelete(url);
    case Method.POST: {
        HttpPost postRequest = new HttpPost(url);
        //                postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(url);
        //                putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(url);
    case Method.OPTIONS:
        return new HttpOptions(url);
    case Method.TRACE:
        return new HttpTrace(url);
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(url);
        //                patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:org.apache.cxf.systest.jaxrs.cors.CrossOriginSimpleTest.java

@Test
public void preflightPostClassAnnotationFail() throws ClientProtocolException, IOException {
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions httpoptions = new HttpOptions("http://localhost:" + PORT + "/antest/unannotatedPost");
    httpoptions.addHeader("Origin", "http://in.org");
    // nonsimple header
    httpoptions.addHeader("Content-Type", "application/json");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "POST");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, "X-custom-1");
    HttpResponse response = httpclient.execute(httpoptions);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN).length);
    assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS).length);
    assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS).length);
    if (httpclient instanceof Closeable) {
        ((Closeable) httpclient).close();
    }/*from   w  w w.j  a  va 2s. com*/

}

From source file:com.base.net.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*w  w w . j  a  va  2 s  . c o  m*/
@SuppressWarnings("deprecation")
/* protected */static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards
        // compatibility.
        // If the request's post body is null, then the assumption is that
        // the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}