Example usage for org.apache.http.entity BasicHttpEntity BasicHttpEntity

List of usage examples for org.apache.http.entity BasicHttpEntity BasicHttpEntity

Introduction

In this page you can find the example usage for org.apache.http.entity BasicHttpEntity BasicHttpEntity.

Prototype

public BasicHttpEntity() 

Source Link

Usage

From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java

@Test
public void testS_C1_FC_OB__C1_XC__C2_AC__EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*  ww  w .ja  v  a2 s. c  om*/
    PartiallyRepeatableHttpEntity replay;
    InputStream stream;
    String text;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes(UTF8)));
    replay = new PartiallyRepeatableHttpEntity(basic, 5);

    stream = replay.getContent();
    text = byteRead(stream, -1);
    assertThat(text, is("0123456789"));
    stream.close();

    try {
        replay.getContent();
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }
}

From source file:org.fcrepo.integration.AbstractResourceIT.java

protected static void addMixin(final String pid, final String mixinUrl) throws IOException {
    final HttpPatch updateObjectGraphMethod = new HttpPatch(serverAddress + pid);
    updateObjectGraphMethod.addHeader(CONTENT_TYPE, "application/sparql-update");
    final BasicHttpEntity e = new BasicHttpEntity();

    e.setContent(new ByteArrayInputStream(
            ("INSERT DATA { <> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" + mixinUrl + "> . } ")
                    .getBytes()));/*from   w  ww .ja v a  2s .  c  om*/
    updateObjectGraphMethod.setEntity(e);
    final HttpResponse response = client.execute(updateObjectGraphMethod);
    assertEquals(NO_CONTENT.getStatusCode(), response.getStatusLine().getStatusCode());
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testB_C1_FC_OB__C1_XC__C2_AC_EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;/*from   w  w  w  .  j ava  2  s  .c  om*/
    CappedBufferHttpEntity replay;
    InputStream stream;
    String text;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes(UTF8)));
    replay = new CappedBufferHttpEntity(basic, 5);

    stream = replay.getContent();
    try {
        text = blockRead(stream, UTF8, -1, 3);
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }
}

From source file:org.apache.hadoop.gateway.dispatch.PartiallyRepeatableHttpEntityTest.java

@Test
public void testB_C1_FC_OB__C1_XC__C2_AC_EE() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;//www  .  j a v a2  s .  c o m
    PartiallyRepeatableHttpEntity replay;
    InputStream stream;
    String text;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes(UTF8)));
    replay = new PartiallyRepeatableHttpEntity(basic, 5);

    stream = replay.getContent();
    text = blockRead(stream, UTF8, -1, 3);
    assertThat(text, is("0123456789"));
    stream.close();

    try {
        replay.getContent();
        fail("Expected IOException");
    } catch (IOException e) {
        // Expected.
    }
}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.HttpRequestFactory.java

private static HttpPost addPageHttpPost(String confluenceRestApiEndpoint, PagePayload pagePayload) {
    String jsonPayload = toJsonString(pagePayload);
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(jsonPayload.getBytes()));

    HttpPost postRequest = new HttpPost(confluenceRestApiEndpoint + "/content");
    postRequest.setEntity(entity);//from ww  w  .  j a  v a2s  . co  m
    postRequest.addHeader(APPLICATION_JSON_UTF8_HEADER);

    return postRequest;
}

From source file:org.apache.hadoop.gateway.dispatch.CappedBufferHttpEntityTest.java

@Test
public void testS_C1_PC_IB() throws IOException {
    String data = "0123456789";
    BasicHttpEntity basic;//from  w w  w.  j ava 2 s. c  o m
    CappedBufferHttpEntity replay;
    InputStream stream;
    String text;

    basic = new BasicHttpEntity();
    basic.setContent(new ByteArrayInputStream(data.getBytes(UTF8)));
    replay = new CappedBufferHttpEntity(basic, 20);

    stream = replay.getContent();
    text = byteRead(stream, 3);
    assertThat(text, is("012"));
}

From source file:at.uni_salzburg.cs.ckgroup.cpcc.mapper.algorithms.RandomTaskGenerator.java

private void addActionPointToVirtualVehicle(IVirtualVehicleInfo vehicleInfo, ActionPoint ap)
        throws IOException {

    String url = vehicleInfo.getEngineUrl() + "/vehicle/text/vehicleAddTask/" + vehicleInfo.getVehicleName();

    HttpPost httppost = new HttpPost(url);
    BasicHttpEntity entity1 = new BasicHttpEntity();
    entity1.setContent(ap.getInputStream());
    httppost.setEntity(entity1);/*from  w w w .  ja  va 2  s.com*/

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(httppost);

    LOG.info("Add AP: " + response.getStatusLine());
}

From source file:org.geoserver.wms.map.MockHttpClientConnectionManager.java

@Override
public ConnectionRequest requestConnection(HttpRoute arg0, Object arg1) {
    return new ConnectionRequest() {

        @Override/*from   w  ww . jav  a 2s. c  o m*/
        public boolean cancel() {
            return false;
        }

        @Override
        public HttpClientConnection get(long arg0, TimeUnit arg1)
                throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
            connections++;
            return new HttpClientConnection() {

                @Override
                public void shutdown() throws IOException {
                }

                @Override
                public void setSocketTimeout(int arg0) {
                }

                @Override
                public boolean isStale() {
                    return false;
                }

                @Override
                public boolean isOpen() {
                    return true;
                }

                @Override
                public int getSocketTimeout() {
                    return 0;
                }

                @Override
                public HttpConnectionMetrics getMetrics() {
                    return null;
                }

                @Override
                public void close() throws IOException {
                }

                @Override
                public void sendRequestHeader(HttpRequest arg0) throws HttpException, IOException {
                }

                @Override
                public void sendRequestEntity(HttpEntityEnclosingRequest arg0)
                        throws HttpException, IOException {
                }

                @Override
                public HttpResponse receiveResponseHeader() throws HttpException, IOException {
                    return new HttpResponse() {

                        List<Header> headers = new ArrayList<Header>();

                        @Override
                        public void addHeader(Header arg0) {
                        }

                        @Override
                        public void addHeader(String arg0, String arg1) {
                        }

                        @Override
                        public boolean containsHeader(String arg0) {
                            return false;
                        }

                        @Override
                        public Header[] getAllHeaders() {
                            return headers.toArray(new Header[] {});
                        }

                        public Header getHeader(String header) {
                            if ("transfer-encoding".equalsIgnoreCase(header)) {
                                return new BasicHeader(header, "identity");
                            }
                            if ("date".equalsIgnoreCase(header)) {

                                return new BasicHeader(header,
                                        dateFormat.format(new GregorianCalendar().getTime()));
                            }
                            if ("cache-control".equalsIgnoreCase(header)) {
                                return new BasicHeader(header, enableCache ? "public" : "no-cache");
                            }
                            if ("content-length".equalsIgnoreCase(header)) {
                                return new BasicHeader(header, response.length() + "");
                            }
                            if ("content-encoding".equalsIgnoreCase(header)) {
                                return new BasicHeader(header, "identity");
                            }
                            if ("age".equalsIgnoreCase(header)) {
                                return new BasicHeader(header, "0");
                            }
                            if ("expires".equalsIgnoreCase(header) && enableCache) {
                                GregorianCalendar expires = new GregorianCalendar();
                                expires.add(GregorianCalendar.MINUTE, 30);
                                return new BasicHeader(header, dateFormat.format(expires.getTime()));
                            }
                            return new BasicHeader(header, "");
                        }

                        @Override
                        public Header getFirstHeader(String header) {
                            Header value = getHeader(header);
                            headers.add(value);
                            return value;
                        }

                        @Override
                        public Header[] getHeaders(String header) {

                            return new Header[] { getFirstHeader(header) };
                        }

                        @Override
                        public Header getLastHeader(String header) {
                            return new BasicHeader(header, "");
                        }

                        @Override
                        public HttpParams getParams() {
                            // TODO Auto-generated method stub
                            return null;
                        }

                        @Override
                        public ProtocolVersion getProtocolVersion() {
                            return HttpVersion.HTTP_1_1;
                        }

                        @Override
                        public HeaderIterator headerIterator() {
                            return new BasicHeaderIterator(headers.toArray(new Header[] {}), "mock");
                        }

                        @Override
                        public HeaderIterator headerIterator(String header) {
                            return new BasicHeaderIterator(headers.toArray(new Header[] {}), "mock");
                        }

                        @Override
                        public void removeHeader(Header arg0) {
                        }

                        @Override
                        public void removeHeaders(String arg0) {
                        }

                        @Override
                        public void setHeader(Header arg0) {
                        }

                        @Override
                        public void setHeader(String arg0, String arg1) {
                        }

                        @Override
                        public void setHeaders(Header[] arg0) {
                        }

                        @Override
                        public void setParams(HttpParams arg0) {
                        }

                        @Override
                        public HttpEntity getEntity() {
                            BasicHttpEntity entity = new BasicHttpEntity();
                            entity.setContentLength(response.length());
                            entity.setContent(
                                    new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
                            return entity;
                        }

                        @Override
                        public Locale getLocale() {
                            return Locale.ENGLISH;
                        }

                        @Override
                        public StatusLine getStatusLine() {
                            return new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK");
                        }

                        @Override
                        public void setEntity(HttpEntity arg0) {
                        }

                        @Override
                        public void setLocale(Locale arg0) {
                        }

                        @Override
                        public void setReasonPhrase(String arg0) throws IllegalStateException {
                        }

                        @Override
                        public void setStatusCode(int arg0) throws IllegalStateException {
                        }

                        @Override
                        public void setStatusLine(StatusLine arg0) {
                        }

                        @Override
                        public void setStatusLine(ProtocolVersion arg0, int arg1) {
                        }

                        @Override
                        public void setStatusLine(ProtocolVersion arg0, int arg1, String arg2) {
                        }
                    };
                }

                @Override
                public void receiveResponseEntity(HttpResponse arg0) throws HttpException, IOException {
                }

                @Override
                public boolean isResponseAvailable(int arg0) throws IOException {
                    return true;
                }

                @Override
                public void flush() throws IOException {
                }
            };
        }
    };
}

From source file:ru.apertum.qsystem.reports.model.QReportsList.java

public synchronized byte[] generate(QUser user, String uri, HashMap<String, String> params) {
    final HttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST", uri);
    r.addHeader("Cookie", "username=" + user.getName() + "; password=" + user.getPassword());
    final StringBuilder sb = new StringBuilder();
    params.keySet().stream().forEach((st) -> {
        sb.append("&").append(st).append("=").append(params.get(st));
    });/*from  w ww  . j  a  va 2s .co m*/
    final InputStream is = new ByteArrayInputStream(sb.substring(1).getBytes());
    final BasicHttpEntity b = new BasicHttpEntity();
    b.setContent(is);
    r.setEntity(b);
    sb.setLength(0);
    return generate(r).getData();
}

From source file:com.aerofs.baseline.http.TestHttpRequestHandler.java

private static HttpPost newChunkedPost(String resource, byte[] bytes) {
    BasicHttpEntity basic = new BasicHttpEntity();
    basic.setChunked(true);//from   ww w . j a v a 2 s.co m
    basic.setContentLength(-1);
    basic.setContent(new ByteArrayInputStream(bytes));

    HttpPost post = new HttpPost(ServiceConfiguration.SERVICE_URL + "/" + resource);
    post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
    post.setEntity(basic);
    return post;
}