List of usage examples for org.apache.http.entity ContentProducer ContentProducer
ContentProducer
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 w w w . j av a 2 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 w w. ja v a 2s .c om } }); 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);/*from w ww. j a va 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 .j ava 2 s. c om*/ 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 www . j a va 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 .j a v a 2s . 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 ww w . j a va 2 s. c om writer.flush(); } }); }
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 ww w . j a va 2 s. co m 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 av a 2 s. c om 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.wentam.defcol.connect_to_computer.HomeCommandHandler.java
@Override public void handle(final HttpRequest request, final HttpResponse response, HttpContext httpContext) throws HttpException, IOException { HttpEntity entity = new EntityTemplate(new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); String req = request.getRequestLine().getUri(); req = req.replaceAll("/\\?", ""); String[] pairs = req.split("&"); HashMap data = new HashMap(); for (int i = 0; i < pairs.length; i++) { if (pairs[i].contains("=")) { String[] pair = pairs[i].split("="); data.put(pair[0], pair[1]); }//from w w w . j a v a 2 s . c om } String action = "none"; if (data.containsKey("action")) { action = (String) data.get("action"); } String resp = "404 on " + action; if (action.equals("none") || action.equals("home")) { response.setHeader("Content-Type", "text/html"); resp = getHtml(); } else if (action.equals("getPalettes")) { response.setHeader("Content-Type", "application/json"); JSONArray json = new JSONArray(); int tmp[] = { 0 }; ArrayList<String> palettes = pFile.getRows(tmp); Iterator i = palettes.iterator(); while (i.hasNext()) { JSONObject item = new JSONObject(); try { item.put("name", i.next()); } catch (JSONException e) { } json.put(item); } resp = json.toString(); } else if (action.equals("getJquery")) { response.setHeader("Content-Type", "application/javascript"); resp = jquery; } else if (action.equals("getJs")) { response.setHeader("Content-Type", "application/javascript"); resp = getJs(); } else if (action.equals("getPaletteColors")) { response.setHeader("Content-Type", "application/javascript"); int id = Integer.parseInt((String) data.get("id")); int tmp[] = { 1 }; String row = pFile.getRow(id, tmp); String colors[] = row.split("\\."); JSONArray json = new JSONArray(); for (int i = 0; i < colors.length; i++) { json.put(colors[i]); } resp = json.toString(); } writer.write(resp); writer.flush(); } }); response.setEntity(entity); }