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

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

Introduction

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

Prototype

public EntityTemplate(ContentProducer contentProducer) 

Source Link

Usage

From source file:com.wondershare.http.server.impl.HomePageServlet.java

@Override
protected void doGet(HttpRequest request, HttpResponse response, HttpContext context)
        throws IOException, ServletException {
    HttpEntity entity = new EntityTemplate(new ContentProducer() {
        @Override//from  www.  j a va2 s .c  o  m
        public void writeTo(OutputStream outstream) throws IOException {
            OutputStreamWriter out = new OutputStreamWriter(outstream);
            out.write(Utils.openHTMLString(mContext, R.raw.home));
            out.flush();
        }
    });
    ((EntityTemplate) entity).setContentType("text/html");
    response.setEntity(entity);
}

From source file:com.starbucks.apps.HttpUtils.java

private static HttpInvocationContext invoke(HttpUriRequest request, final String payload,
        final String contentType) throws IOException {

    if (payload != null) {
        HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
        EntityTemplate ent = new EntityTemplate(new ContentProducer() {
            public void writeTo(OutputStream outputStream) throws IOException {
                outputStream.write(payload.getBytes());
                outputStream.flush();//from  w  ww .j  ava2 s . c  o m
            }
        });
        ent.setContentType(contentType);
        entityEncReq.setEntity(ent);
    }

    HttpInvocationContext context = new HttpInvocationContext(payload);
    DefaultHttpClient client = getHttpClient(context);
    HttpResponse response = client.execute(request);
    context.setHttpResponse(response);
    return context;
}

From source file:com.wso2.raspberrypi.apicalls.HttpClient.java

public HttpResponse doPost(String url, String token, final String payload, String contentType)
        throws IOException {
    HttpUriRequest request = new HttpPost(url);
    addSecurityHeaders(request, token);/*w  w w.ja  v a 2s . com*/

    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            outputStream.write(payload.getBytes());
            outputStream.flush();
        }
    });
    ent.setContentType(contentType);
    entityEncReq.setEntity(ent);
    return client.execute(request);
}

From source file:com.personalserver.HomeCommandHandler.java

@Override
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext)
        throws HttpException, IOException {

    this.mHost = httpRequest.getFirstHeader("Host").getValue();
    System.out.println("Host : " + mHost);
    HttpEntity entity = new EntityTemplate(new ContentProducer() {
        public void writeTo(final OutputStream outstream) throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
            String html = "<html><head></head>" + "<body><center><h1>Welcome to Personal Server<h1></center>"
                    + "<p>to browse file click <a href=\"http://" + mHost + "/dir\">here</a></p>"
                    + "</body></html>";

            // html = AssetsUtils.readHtmlForName(mContext, "home");

            writer.write(html);//w  w w.ja  v  a 2s . c  o  m
            writer.flush();
        }
    });

    httpResponse.setHeader("Content-Type", "text/html");
    httpResponse.setEntity(entity);
}

From source file:com.wondershare.http.server.impl.CallogServlet.java

@Override
protected void doGet(HttpRequest request, HttpResponse response, HttpContext context)
        throws IOException, ServletException {
    Cursor cursor = mContext.getContentResolver().query(callogUri, SysConst.CALLOG_PROJECTION, null, null,
            null);//from ww  w. j  ava 2  s .c  o  m
    final String html = Utils.spellCallogToHTML(cursor);
    HttpEntity entity = new EntityTemplate(new ContentProducer() {
        @Override
        public void writeTo(OutputStream outstream) throws IOException {
            OutputStreamWriter out = new OutputStreamWriter(outstream);
            out.write(html);
            out.flush();
        }
    });
    ((EntityTemplate) entity).setContentType("text/html");
    response.setEntity(entity);
}

From source file:org.eclipse.koneki.protocols.omadm.client.http.internal.DMHttpClient.java

@Override
protected void sendAndReceiveMessage(final URI server, final String encoding, final DMMessenger messenger)
        throws IOException, DMClientException {
    try {/*from w  w  w.  java2  s .  c  o  m*/
        final HttpPost post = new HttpPost(server);

        final EntityTemplate entity = new EntityTemplate(new ContentProducer() {

            @Override
            public void writeTo(final OutputStream out) throws IOException {
                try {
                    messenger.writeMessage(out);
                } catch (final DMClientException e) {
                    throw new IOException(e);
                }
            }

        });
        entity.setChunked(false);
        entity.setContentEncoding(encoding);
        entity.setContentType("application/vnd.syncml.dm+xml"); //$NON-NLS-1$
        post.setEntity(entity);

        final HttpResponse response = this.httpClient.execute(post);

        if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) {
            throw new DMClientException(response.getStatusLine().toString());
        }

        messenger.readMessage(response.getEntity().getContent());

        EntityUtils.consume(response.getEntity());
    } catch (final IOException e) {
        if (e.getCause() != null && e.getCause() instanceof DMClientException) {
            throw (DMClientException) e.getCause();
        } else {
            throw e;
        }
    }
}

From source file:org.wso2.carbon.automation.extensions.servers.httpserver.TestRequestHandler.java

private EntityTemplate createEntity() {
    return new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
            writer.write(payload);/*from  w w  w . j a  v  a2s .c  o m*/
            writer.flush();
        }
    });
}

From source file:org.robertburrelldonkin.template4couchdb.rest.HttpClientRestClient.java

@Override
public <R, D> R post(String url, IDocumentMarshaller<D> documentMarshaller, D document,
        IDocumentUnmarshaller<R> responseUnmarshaller) {
    final ContentProducer producer = codecFactory.producerFor(documentMarshaller, document);
    final EntityTemplate entity = new EntityTemplate(producer);
    entity.setContentType(ContentType.APPLICATION_JSON.toString());
    final ResponseHandler<R> handler = codecFactory.handlerFor(responseUnmarshaller);
    final HttpPost post = new HttpPost(url);
    post.setEntity(entity);//from  w w  w . ja va 2  s .co  m
    final R response;
    try {
        response = httpClient.execute(post, handler);
    } catch (ClientProtocolException e) {
        throw new HttpClientRestClientException(e);
    } catch (IOException e) {
        throw new HttpClientRestClientException(e);
    }
    return response;
}

From source file:com.personalserver.DirCommandHandler.java

private HttpEntity getEntityFromUri(String uri, HttpResponse response) {
    String contentType = "text/html";
    String filepath = FOLDER_SHARE_PATH;

    if (uri.equalsIgnoreCase("/") || uri.length() <= 0) {
        filepath = FOLDER_SHARE_PATH + "/";
    } else {//from w  w  w  .  j  a v  a2  s .  com
        filepath = FOLDER_SHARE_PATH + "/" + URLDecoder.decode(uri);
    }

    System.out.println("request uri : " + uri);
    System.out.println("FOLDER SHARE PATH : " + FOLDER_SHARE_PATH);
    System.out.println("filepath : " + filepath);

    final File file = new File(filepath);

    HttpEntity entity = null;

    if (file.isDirectory()) {
        entity = new EntityTemplate(new ContentProducer() {
            public void writeTo(final OutputStream outstream) throws IOException {
                OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
                String resp = getDirListingHTML(file);

                writer.write(resp);
                writer.flush();
            }
        });

        response.setHeader("Content-Type", contentType);
    } else if (file.exists()) {
        contentType = URLConnection.guessContentTypeFromName(file.getAbsolutePath());

        entity = new FileEntity(file, contentType);

        response.setHeader("Content-Type", contentType);
    } else {
        entity = new EntityTemplate(new ContentProducer() {
            public void writeTo(final OutputStream outstream) throws IOException {
                OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
                String resp = "<html>" + "<head><title>ERROR : NOT FOUND</title></head>" + "<body>"
                        + "<center><h1>FILE OR DIRECTORY NOT FOUND !</h1></center>"
                        + "<p>Sorry, file or directory you request not available<br />"
                        + "Contact your administrator<br />" + "</p>" + "</body></html>";

                writer.write(resp);
                writer.flush();
            }
        });
        response.setHeader("Content-Type", "text/html");
    }

    return entity;
}

From source file:com.wso2.raspberrypi.apicalls.HttpClient.java

public HttpResponse doPut(String url, String token, final String payload, String contentType)
        throws IOException {
    HttpUriRequest request = new HttpPut(url);
    addSecurityHeaders(request, token);/*from w w w .  j a v  a2 s  . c  o m*/

    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            outputStream.write(payload.getBytes());
            outputStream.flush();
        }
    });
    ent.setContentType(contentType);
    entityEncReq.setEntity(ent);
    return client.execute(request);
}