List of usage examples for org.apache.http.entity EntityTemplate setContentType
public void setContentType(Header header)
From source file:org.openjena.riot.web.HttpOp.java
/** POST with response body */ public static void execHttpPost(String url, String contentType, ContentProducer provider, String acceptType, Map<String, HttpResponseHandler> handlers) { EntityTemplate entity = new EntityTemplate(provider); entity.setContentType(contentType); execHttpPost(url, entity, acceptType, handlers); }
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();/*ww w .j a v a 2 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:org.apache.jena.fuseki.embedded.TestEmbeddedFuseki.java
/** Create an HttpEntity for the graph */ protected static HttpEntity graphToHttpEntity(final Graph graph) { final RDFFormat syntax = RDFFormat.TURTLE_BLOCKS; ContentProducer producer = new ContentProducer() { @Override/* www . j a v a 2 s. co m*/ public void writeTo(OutputStream out) { RDFDataMgr.write(out, graph, syntax); } }; EntityTemplate entity = new EntityTemplate(producer); ContentType ct = syntax.getLang().getContentType(); entity.setContentType(ct.getContentType()); return entity; }
From source file:org.wso2.carbon.automation.extensions.servers.httpserver.TestRequestHandler.java
private void writeContent(HttpRequest request, HttpResponse response) { // Check for edge cases as stated in the HTTP specs if ("HEAD".equals(request.getRequestLine().getMethod()) || statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_RESET_CONTENT || statusCode == HttpStatus.SC_NOT_MODIFIED) { return;/* w w w.j a va 2 s . c o m*/ } EntityTemplate body = createEntity(); body.setContentType(contentType); response.setEntity(body); }
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 www .j a v a 2s . c o 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.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 w w .j a v a 2 s . 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.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);/* www . j a va 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:org.apache.jena.web.DatasetGraphAccessorHTTP.java
/** Create an HttpEntity for the graph */ protected HttpEntity graphToHttpEntity(final Graph graph) { final RDFFormat syntax = getOutboundSyntax(); ContentProducer producer = new ContentProducer() { @Override//from w w w . ja v a 2 s . co m public void writeTo(OutputStream out) { RDFDataMgr.write(out, graph, syntax); } }; EntityTemplate entity = new EntityTemplate(producer); String ct = syntax.getLang().getContentType().getContentType(); entity.setContentType(ct); return entity; }
From source file:ru.apertum.qsystem.reports.net.HttpQSystemReportsHandler.java
@Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) { throw new MethodNotSupportedException(method + " method not supported"); }// w ww . j ava2 s. c o m // ?? ? Response result = QReportsList.getInstance().generate(request); // ? if (result == null) { String subject = NetUtil.getUrl(request); if ("/".equals(subject)) { subject = "/login.html"; } int dot = subject.lastIndexOf("."); final String ext = subject.substring(dot + 1); String contentType = "; charset=UTF-8"; if ("htm".equals(ext) || "html".equals(ext)) { contentType = "text/" + ext + contentType; } else { if ("pdf".equals(ext) || "rtf".equals(ext) || "doc".equals(ext) || "xlsx".equals(ext)) { contentType = "application/" + ext + contentType; } else { if ("gif".equals(ext) || "jpeg".equals(ext) || "jpg".equals(ext) || "ico".equals(ext) || "xpm".equals(ext)) { contentType = "image/" + ext + contentType; } } } // ?? "/ru/apertum/qsystem/reports/web/" final InputStream inStream = getClass() .getResourceAsStream("/ru/apertum/qsystem/reports/web" + subject); if (inStream == null) { QLog.l().logRep() .warn("?? : \"/ru/apertum/qsystem/reports/web" + subject + "\""); // ??, // subject = subject.substring(1); File anyFile = new File(subject); if (anyFile.exists()) { QLog.l().logRep().info(" : \"" + subject + "\""); FileInputStream fInStream = null; try { fInStream = new FileInputStream(anyFile); } catch (FileNotFoundException ex) { final String err = " \"" + subject + "\" "; QLog.l().logRep().error("err " + ex); result = new Response((err + ex).getBytes(), contentType); response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } try { result = new Response(Uses.readInputStream(fInStream), contentType); response.setStatusCode(HttpStatus.SC_OK); } catch (IOException ex) { final String err = " \"" + subject + "\" "; QLog.l().logRep().error("err " + ex); result = new Response((err + ex).getBytes(), contentType); response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } } else { // anyFile = new File( Uses.TEMP_FOLDER + File.separator + "temphtml.html_files" + File.separator + subject); if (anyFile.exists()) { QLog.l().logRep().info(" : \"" + Uses.TEMP_FOLDER + File.separator + "temphtml.html_files" + File.separator + subject + "\""); FileInputStream fInStream = null; try { fInStream = new FileInputStream(anyFile); } catch (FileNotFoundException ex) { final String err = " \"" + Uses.TEMP_FOLDER + File.separator + "temphtml.html_files" + File.separator + subject + "\" "; QLog.l().logRep().error("err " + ex); result = new Response((err + ex).getBytes(), contentType); response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } try { result = new Response(Uses.readInputStream(fInStream), contentType); response.setStatusCode(HttpStatus.SC_OK); } catch (IOException ex) { final String err = " \"" + Uses.TEMP_FOLDER + File.separator + "temphtml.html_files" + File.separator + subject + "\" "; QLog.l().logRep().error("err " + ex); result = new Response((err + ex).getBytes(), contentType); response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } anyFile.delete(); } else { // . ? ?? ( ???) QLog.l().logRep() .error("?? : \"" + Uses.TEMP_FOLDER + File.separator + "temphtml.html_files" + File.separator + subject + "\""); final String s = "<html><head><meta http-equiv = \"Content-Type\" content = \"text/html; charset=utf-8\" ></head><p align=center>?? .</p></html>"; result = new Response(s.getBytes()); } } } else { QLog.l().logRep().info(" ??: \"" + subject + "\""); try { result = new Response(Uses.readInputStream(inStream)); if ("/login.html".equals(subject)) { result.setData( RepResBundle.getInstance().prepareString(new String(result.getData(), "UTF-8")) .replaceFirst(Uses.ANCHOR_USERS_FOR_REPORT, QReportsList.getInstance().getHtmlUsersList()) .replaceFirst(Uses.ANCHOR_PROJECT_NAME_FOR_REPORT, Uses.getLocaleMessage("project.name" + FAbout.getCMRC_SUFF())) .getBytes("UTF-8")); //"Cp1251" } } catch (IOException ex) { QLog.l().logRep().error(" ? ??. " + ex); result = new Response((" ? ??. " + ex).getBytes()); response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } response.setStatusCode(HttpStatus.SC_OK); } } // : QLog.l().logRep().trace(" " + result.getData().length + " ? \"" + request.getRequestLine().getUri() + "\"."); final byte[] result2 = result.getData(); final EntityTemplate body = new EntityTemplate((final OutputStream outstream) -> { outstream.write(result2); outstream.flush(); }); body.setContentType(result.getContentType()); response.setEntity(body); NetUtil.freeEntityContent(request); }
From source file:org.expath.httpclient.impl.ApacheHttpConnection.java
/** * Configure the request to get its entity body from the {@link HttpRequestBody}. *//*from w w w .j a va2 s. co m*/ private void setRequestEntity(HttpRequestBody body) throws HttpClientException { if (body == null) { return; } // make the entity from a new producer HttpEntity entity; if (myVersion == HttpVersion.HTTP_1_1) { // Take advantage of HTTP 1.1 chunked encoding to stream the // payload directly to the request. ContentProducer producer = new RequestBodyProducer(body); EntityTemplate template = new EntityTemplate(producer); template.setContentType(body.getContentType()); entity = template; } else { // With HTTP 1.0, chunked encoding is not supported, so first // serialize into memory and use the resulting byte array as the // entity payload. ByteArrayOutputStream buffer = new ByteArrayOutputStream(); body.serialize(buffer); entity = new ByteArrayEntity(buffer.toByteArray()); } // cast the request HttpEntityEnclosingRequestBase req = null; if (!(myRequest instanceof HttpEntityEnclosingRequestBase)) { String msg = "Body not allowed on a " + myRequest.getMethod() + " request"; throw new HttpClientException(msg); } else { req = (HttpEntityEnclosingRequestBase) myRequest; } // set the entity on the request req.setEntity(entity); }