本文整理汇总了Java中org.apache.http.entity.ContentProducer类的典型用法代码示例。如果您正苦于以下问题:Java ContentProducer类的具体用法?Java ContentProducer怎么用?Java ContentProducer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ContentProducer类属于org.apache.http.entity包,在下文中一共展示了ContentProducer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
/**
* Send a HTTP POST request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPost(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPost(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例2: doPatch
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
/**
* Send a HTTP PATCH request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPatch(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPatch(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例3: doPut
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
/**
* Send a HTTP PUT request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPut(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPut(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例4: handle
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("GET") && !method.equals("HEAD")) {
throw new MethodNotSupportedException(method + " method not supported");
}
final EntityTemplate body = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write(mJSON);
writer.flush();
}
});
response.setStatusCode(HttpStatus.SC_OK);
body.setContentType("text/json; charset=UTF-8");
response.setEntity(body);
}
示例5: handle
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
public void handle(HttpRequest request, HttpResponse response, HttpContext arg2) throws HttpException, IOException {
if (request.getRequestLine().getMethod().equals("POST")) {
// Retrieve the POST content
HttpEntityEnclosingRequest post = (HttpEntityEnclosingRequest) request;
byte[] entityContent = EntityUtils.toByteArray(post.getEntity());
String content = new String(entityContent, Charset.forName("UTF-8"));
// Execute the request
final String json = RequestHandler.handle(content);
// Return the response
EntityTemplate body = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write(json);
writer.flush();
}
});
response.setStatusCode(HttpStatus.SC_OK);
body.setContentType("application/json; charset=UTF-8");
response.setEntity(body);
}
}
示例6: toContentProducer
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
/**
* Wraps an XML writing into a ContentProducer for
* lazy serialization.
* @param doc the XML document to wrap.
* @return The ContentProducer.
*/
public static ContentProducer toContentProducer(final Document doc) {
return out -> {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, UTF8);
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.transform(
new DOMSource(doc), new StreamResult(out));
} catch (TransformerException te) {
throw new IOException(te);
}
};
}
示例7: doPost
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
/**
* Send a HTTP POST request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPost(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPost(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes(Charset.defaultCharset()));
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例8: doPut
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
/**
* Send a HTTP PUT request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doPut(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpPut(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes(Charset.defaultCharset()));
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
示例9: doOptions
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
/**
* Send a HTTP OPTIONS request to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @param payload Content payload that should be sent
* @param contentType Content-type of the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doOptions(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
HttpUriRequest request = new HttpOptions(url);
setHeaders(headers, request);
if(payload != null) {
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));
EntityTemplate ent = new EntityTemplate(new ContentProducer() {
public void writeTo(OutputStream outputStream) throws IOException {
OutputStream out = outputStream;
if (zip) {
out = new GZIPOutputStream(outputStream);
}
out.write(payload.getBytes());
out.flush();
out.close();
}
});
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
}
return client.execute(request);
}
示例10: newContentProducer
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
protected ContentProducer newContentProducer(String contentType, List docs) {
if ("text/csv".equals(contentType)) {
return new CsvContentProducer(docs);
} else if (PIPELINE_DOC_TYPE.equals(contentType) || "application/json".equals(contentType)) {
return new JacksonContentProducer(jsonObjectMapper, docs);
} else {
throw new IllegalArgumentException("Content type "+contentType+" not supported! Use text/csv or "+PIPELINE_DOC_TYPE);
}
}
示例11: toApacheContentProducer
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
private ContentProducer toApacheContentProducer(RepeatableContentProducer repeatableContentProducer)
{
return (OutputStream outputStream) -> {
try (InputStream inputStream = repeatableContentProducer.getInputStream()) {
copyLarge(inputStream, outputStream);
}
};
}
示例12: handle
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
String contentType;
Integer code;
Uri uri = Uri.parse(request.getRequestLine().getUri());
String fileUri = URLDecoder.decode(uri.getLastPathSegment());
final byte[] r;
byte[] resp;
AssetManager mgr = context.getAssets();
try {
resp = Utility.loadInputStreamAsByte(mgr.open(fileUri));
contentType = Utility.getMimeTypeForFile(fileUri);
code = HttpStatus.OK.getCode();
} catch (IOException e){
resp = Utility.loadInputStreamAsByte(mgr.open("notfound.html"));
contentType = Utility.MIME_TYPES.get("html");
code = HttpStatus.NOT_FOUND.getCode();
}
r=resp;
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
outstream.write(r);
}
});
((EntityTemplate)entity).setContentType(contentType);
response.setStatusCode(code);
response.setEntity(entity);
}
示例13: handle
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
String contentType;
Integer code;
Uri uri = Uri.parse(request.getRequestLine().getUri());
String folder;
List<String> s = uri.getPathSegments();
String path = File.separator;
for(int i=1;i<s.size()-1;i++) {
path+=s.get(i)+File.separator;
}
folder = path;
final File file = new File(Utility.BLACKHOLE_PATH +folder+s.get(s.size()-1));
final byte[] r;
byte[] resp;
if(file.exists()){
resp = Utility.loadFileAsByte(file.getAbsolutePath());
contentType = Utility.getMimeTypeForFile(file.getName());
code = HttpStatus.OK.getCode();
}else{
AssetManager mgr = context.getAssets();
resp = Utility.loadInputStreamAsByte(mgr.open("notfound.html"));
contentType = Utility.MIME_TYPES.get("html");
code = HttpStatus.NOT_FOUND.getCode();
}
r=resp;
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
outstream.write(r);
}
});
((EntityTemplate)entity).setContentType(contentType);
response.setStatusCode(code);
response.addHeader(HttpHeader.CONTENT_DISPOSITION, "attachment");
response.setEntity(entity);
}
示例14: forgeResponse
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
public HttpEntity forgeResponse(){
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
outstream.write(response);
}
});
((EntityTemplate)entity).setContentType(contentType);
return entity;
}
示例15: newContentProducer
import org.apache.http.entity.ContentProducer; //导入依赖的package包/类
protected ContentProducer newContentProducer(String contentType, List docs) {
if ("text/csv".equals(contentType)) {
return new CsvContentProducer(docs);
} else if (JSON_CONTENT_TYPE.equals(contentType) || PIPELINE_DOC_CONTENT_TYPE.equals(contentType)) {
return new JacksonContentProducer(jsonObjectMapper, docs);
} else {
throw new IllegalArgumentException("Content type "+contentType+
" not supported! Use text/csv, application/json, or "+PIPELINE_DOC_CONTENT_TYPE);
}
}