Search in sources :

Example 61 with HttpHead

use of org.apache.http.client.methods.HttpHead in project aliyun-oss-java-sdk by aliyun.

the class HttpRequestFactory method createHttpRequest.

public HttpRequestBase createHttpRequest(ServiceClient.Request request, ExecutionContext context) {
    String uri = request.getUri();
    HttpRequestBase httpRequest;
    HttpMethod method = request.getMethod();
    if (method == HttpMethod.POST) {
        HttpPost postMethod = new HttpPost(uri);
        if (request.getContent() != null) {
            postMethod.setEntity(new RepeatableInputStreamEntity(request));
        }
        httpRequest = postMethod;
    } else if (method == HttpMethod.PUT) {
        HttpPut putMethod = new HttpPut(uri);
        if (request.getContent() != null) {
            if (request.isUseChunkEncoding()) {
                putMethod.setEntity(buildChunkedInputStreamEntity(request));
            } else {
                putMethod.setEntity(new RepeatableInputStreamEntity(request));
            }
        }
        httpRequest = putMethod;
    } else if (method == HttpMethod.GET) {
        httpRequest = new HttpGet(uri);
    } else if (method == HttpMethod.DELETE) {
        httpRequest = new HttpDelete(uri);
    } else if (method == HttpMethod.HEAD) {
        httpRequest = new HttpHead(uri);
    } else if (method == HttpMethod.OPTIONS) {
        httpRequest = new HttpOptions(uri);
    } else {
        throw new ClientException("Unknown HTTP method name: " + method.toString());
    }
    configureRequestHeaders(request, context, httpRequest);
    return httpRequest;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) ClientException(com.aliyun.oss.ClientException) HttpMethod(com.aliyun.oss.HttpMethod) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead)

Example 62 with HttpHead

use of org.apache.http.client.methods.HttpHead in project aliyun-oss-java-sdk by aliyun.

the class HttpFactoryTest method testCreateHttpRequest.

@Test
public void testCreateHttpRequest() throws Exception {
    ExecutionContext context = new ExecutionContext();
    String url = "http://127.0.0.1";
    String content = "This is a test request";
    byte[] contentBytes = null;
    try {
        contentBytes = content.getBytes(context.getCharset());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    HttpRequestFactory factory = new HttpRequestFactory();
    ServiceClient.Request request = new ServiceClient.Request();
    request.setUrl(url);
    HttpRequestBase httpRequest = null;
    // GET
    request.setMethod(HttpMethod.GET);
    httpRequest = factory.createHttpRequest(request, context);
    HttpGet getMethod = (HttpGet) httpRequest;
    assertEquals(url, getMethod.getURI().toString());
    // DELETE
    request.setMethod(HttpMethod.DELETE);
    httpRequest = factory.createHttpRequest(request, context);
    HttpDelete delMethod = (HttpDelete) httpRequest;
    assertEquals(url, delMethod.getURI().toString());
    // HEAD
    request.setMethod(HttpMethod.HEAD);
    httpRequest = factory.createHttpRequest(request, context);
    HttpHead headMethod = (HttpHead) httpRequest;
    assertEquals(url, headMethod.getURI().toString());
    // POST
    request.setContent(new ByteArrayInputStream(contentBytes));
    request.setContentLength(contentBytes.length);
    request.setMethod(HttpMethod.POST);
    httpRequest = factory.createHttpRequest(request, context);
    HttpPost postMethod = (HttpPost) httpRequest;
    assertEquals(url, postMethod.getURI().toString());
    HttpEntity entity = postMethod.getEntity();
    try {
        assertEquals(content, readSting(entity.getContent()));
    } catch (IllegalStateException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    // PUT
    request.setContent(new ByteArrayInputStream(contentBytes));
    request.setContentLength(contentBytes.length);
    request.setMethod(HttpMethod.PUT);
    httpRequest = factory.createHttpRequest(request, context);
    HttpPut putMethod = (HttpPut) httpRequest;
    assertEquals(url, putMethod.getURI().toString());
    entity = putMethod.getEntity();
    try {
        assertEquals(content, readSting(entity.getContent()));
    } catch (IllegalStateException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    try {
        request.close();
    } catch (IOException e) {
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 63 with HttpHead

use of org.apache.http.client.methods.HttpHead in project OpenOLAT by OpenOLAT.

the class RestConnection method createHead.

public HttpHead createHead(URI uri, String accept, boolean cookie) {
    HttpHead head = new HttpHead(uri);
    decorateHttpMessage(head, accept, "en", cookie);
    return head;
}
Also used : HttpHead(org.apache.http.client.methods.HttpHead)

Example 64 with HttpHead

use of org.apache.http.client.methods.HttpHead in project OpenOLAT by OpenOLAT.

the class WebDAVConnection method head.

public HttpResponse head(URI uri) throws IOException, URISyntaxException {
    HttpHead propfind = new HttpHead(uri);
    HttpResponse response = execute(propfind);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    return response;
}
Also used : HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 65 with HttpHead

use of org.apache.http.client.methods.HttpHead in project ais-sdk by huaweicloudsdk.

the class AccessServiceImpl method createRequest.

/**
 * Make a request that can be sent by the HTTP client.
 *
 * @param url
 *            specifies the API access path.
 * @param header
 *            specifies the header information to be added.
 * @param content
 *            specifies the body content to be sent in the API call.
 * @param contentLength
 *            specifies the length of the content. This parameter is optional.
 * @param httpMethod
 *            specifies the HTTP method to be used.
 * @return specifies the request that can be sent by an HTTP client.
 */
private static HttpRequestBase createRequest(URL url, Header header, InputStream content, Long contentLength, HttpMethodName httpMethod) {
    HttpRequestBase httpRequest;
    if (httpMethod == HttpMethodName.POST) {
        HttpPost postMethod = new HttpPost(url.toString());
        if (content != null) {
            InputStreamEntity entity = new InputStreamEntity(content, contentLength);
            postMethod.setEntity(entity);
        }
        httpRequest = postMethod;
    } else if (httpMethod == HttpMethodName.PUT) {
        HttpPut putMethod = new HttpPut(url.toString());
        httpRequest = putMethod;
        if (content != null) {
            InputStreamEntity entity = new InputStreamEntity(content, contentLength);
            putMethod.setEntity(entity);
        }
    } else if (httpMethod == HttpMethodName.PATCH) {
        HttpPatch patchMethod = new HttpPatch(url.toString());
        httpRequest = patchMethod;
        if (content != null) {
            InputStreamEntity entity = new InputStreamEntity(content, contentLength);
            patchMethod.setEntity(entity);
        }
    } else if (httpMethod == HttpMethodName.GET) {
        httpRequest = new HttpGet(url.toString());
    } else if (httpMethod == HttpMethodName.DELETE) {
        httpRequest = new HttpDelete(url.toString());
    } else if (httpMethod == HttpMethodName.HEAD) {
        httpRequest = new HttpHead(url.toString());
    } else {
        throw new RuntimeException("Unknown HTTP method name: " + httpMethod);
    }
    httpRequest.addHeader(header);
    return httpRequest;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpHead(org.apache.http.client.methods.HttpHead) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)104 HttpResponse (org.apache.http.HttpResponse)42 HttpGet (org.apache.http.client.methods.HttpGet)30 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)27 IOException (java.io.IOException)26 HttpPut (org.apache.http.client.methods.HttpPut)24 Test (org.junit.Test)24 HttpPost (org.apache.http.client.methods.HttpPost)23 URI (java.net.URI)22 Header (org.apache.http.Header)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 InputStream (java.io.InputStream)13 HttpEntity (org.apache.http.HttpEntity)11 File (java.io.File)9 HttpOptions (org.apache.http.client.methods.HttpOptions)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpPatch (org.apache.http.client.methods.HttpPatch)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6