Java 类org.apache.http.entity.EntityTemplate 实例源码
项目:product-ei
文件:SimpleHttpClient.java
/**
* 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);
}
项目:product-ei
文件:SimpleHttpClient.java
/**
* 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);
}
项目:product-ei
文件:SimpleHttpClient.java
/**
* Send a HTTP DELETE request with entity body to the specified URL
*
* @param url Target endpoint URL
* @param headers Any HTTP headers that should be added to the request
* @return Returned HTTP response
* @throws IOException If an error occurs while making the invocation
*/
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
final String payload, String contentType) throws IOException {
boolean zip = false;
HttpUriRequest request = new HttpDeleteWithEntity(url);
setHeaders(headers, request);
HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
//check if content encoding required
if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
zip = true;
}
EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
ent.setContentType(contentType);
if (zip) {
ent.setContentEncoding("gzip");
}
entityEncReq.setEntity(ent);
return client.execute(request);
}
项目:product-ei
文件:SimpleHttpClient.java
/**
* 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);
}
项目:Android_CCTV
文件:ModInternationalization.java
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);
}
项目:Android_CCTV
文件:CustomHttpServer.java
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);
}
}
项目:carbon-platform-integration
文件:SimpleHttpClient.java
/**
* 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);
}
项目:carbon-platform-integration
文件:SimpleHttpClient.java
/**
* 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);
}
项目:spydroid-ipcamera
文件:ModInternationalization.java
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);
}
项目:spydroid-ipcamera
文件:CustomHttpServer.java
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);
}
}
项目:ecg_over_rtp
文件:ModInternationalization.java
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);
}
项目:ecg_over_rtp
文件:CustomHttpServer.java
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);
}
}
项目:opentravelmate
文件:NativeRequestHandler.java
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
String target = request.getRequestLine().getUri();
String url = URLDecoder.decode(target, "UTF-8");
// Load the required resources
byte[] content;
if (injectedJavaObjectNameByURL.containsKey(url)) {
StringBuilder stringBuilder = new StringBuilder()
.append("define([], function() {\n")
.append(" return window.").append(injectedJavaObjectNameByURL.get(url)).append(";\n")
.append("});");
content = stringBuilder.toString().getBytes("UTF-8");
} else {
InputStream inputStream = findMatchingResource(url);
if (inputStream == null) {
throw new IOException("Unknown resource: " + url);
}
content = IOUtils.toByteArray(inputStream);
}
// Send the resource
EntityTemplate entity = new EntityTemplate(new SimpleContentProducer(content));
response.setEntity(entity);
}
项目:product-ei
文件:SimpleHttpClient.java
/**
* 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);
}
项目:ryf_mms2
文件:RemoteAccessor.java
public String getResponseByStream(String url,String encode,String data,String sessionId)throws Exception{
httpPost = new HttpPost(url);
httpPost.setHeader("sessionId", sessionId);
StreamEntity se = new StreamEntity();
se.data=data;
se.encode=encode;
HttpEntity entity = new EntityTemplate(se);
httpPost.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = httpClient.execute(httpPost, responseHandler);
return content;
}
项目:ryf_mms2
文件:RemoteAccessor.java
public String getResponseByStream(String url,String encode,String data)throws Exception{
httpPost = new HttpPost(url);
StreamEntity se = new StreamEntity();
se.data=data;
se.encode=encode;
HttpEntity entity = new EntityTemplate(se);
httpPost.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = httpClient.execute(httpPost, responseHandler);
return content;
}
项目:Blackhole
文件:AssetHandler.java
@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);
}
项目:Blackhole
文件:FileHandler.java
@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);
}
项目:Blackhole
文件:ResponseForger.java
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;
}
项目:carbon-platform-integration
文件: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;
}
EntityTemplate body = createEntity();
body.setContentType(contentType);
response.setEntity(body);
}
项目:carbon-platform-integration
文件: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);
writer.flush();
}
});
}
项目:marmotta
文件:ResourceClient.java
/**
* Update (overwrite) the metadata of the resource identified by the given uri. The metadata will be serialised to
* application/json and sent to the Apache Marmotta server. The given metadata will override any metadata
* for the resource already existing on the server. The resource has to exist or be created before updating, otherwise
* the method will throw a NotFoundException.
*
* @param uri the URI of the resource to update
* @param metadata the metadata to upload to the resource
* @throws IOException
* @throws MarmottaClientException
*/
public void updateResourceMetadata(final String uri, final Metadata metadata) throws IOException, MarmottaClientException {
HttpClient httpClient = HTTPUtil.createClient(config);
HttpPut put = new HttpPut(getServiceUrl(uri));
put.setHeader(CONTENT_TYPE, "application/rdf+json; rel=meta");
ContentProducer cp = new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
RDFJSONParser.serializeRDFJSON(ImmutableMap.of(uri, metadata), outstream);
}
};
put.setEntity(new EntityTemplate(cp));
try {
HttpResponse response = httpClient.execute(put);
switch(response.getStatusLine().getStatusCode()) {
case 200:
log.debug("metadata for resource {} updated",uri);
break;
case 415:
log.error("server does not support metadata type application/json for resource {}, cannot update", uri);
throw new ContentFormatException("server does not support metadata type application/json for resource "+uri);
case 404:
log.error("resource {} does not exist, cannot update", uri);
throw new NotFoundException("resource "+uri+" does not exist, cannot update");
default:
log.error("error updating resource {}: {} {}",new Object[] {uri,response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()});
throw new MarmottaClientException("error updating resource "+uri+": "+response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
}
} catch (UnsupportedEncodingException e) {
log.error("could not encode URI parameter",e);
throw new MarmottaClientException("could not encode URI parameter");
} finally {
put.releaseConnection();
}
}
项目:marmotta
文件:ImportClient.java
private void uploadDataset(final InputStream in, final String mimeType, HttpClient httpClient) throws IOException, URISyntaxException {
HttpPost post = HTTPUtil.createPost(URL_UPLOAD_SERVICE, config);
post.setHeader(CONTENT_TYPE, mimeType);
ContentProducer cp = new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
ByteStreams.copy(in,outstream);
}
};
post.setEntity(new EntityTemplate(cp));
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
@Override
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
EntityUtils.consume(response.getEntity());
switch(response.getStatusLine().getStatusCode()) {
case 200:
log.debug("dataset uploaded updated successfully");
return true;
case 412:
log.error("mime type {} not acceptable by import service",mimeType);
return false;
default:
log.error("error uploading dataset: {} {}",new Object[] {response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()});
return false;
}
}
};
try {
httpClient.execute(post, handler);
} catch(IOException ex) {
post.abort();
throw ex;
} finally {
post.releaseConnection();
}
}
项目:android-json-http
文件:HttpRequest.java
/**
* 以POST方式请求(上传JSON串)
*
* @param url
* 接口地址
* @param requestBody
* 发送的请求数据包 JSON串
* @return Message对象
*/
public static Message doPost(String url, final String requestBody) {
Log.i(TAG, "POST:REQUEST URL IS --- " + url);
HttpPost httpPost = new HttpPost(url);
ContentProducer contentProducer = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter(outstream, ENCODEING);
writer.write(requestBody);
writer.flush();
}
};
HttpEntity entity = new EntityTemplate(contentProducer);
httpPost.setEntity(entity);
return executeRequest(httpPost);
}
项目:opentravelmate
文件:ImageRequestHandler.java
@Override
public void handle(HttpRequest request, final HttpResponse response, HttpContext context) throws HttpException, IOException {
String urlAsString = request.getRequestLine().getUri();
// Extract the image source and parse the filter parameter
if (!urlAsString.startsWith("/image/source/")) {
throw new HttpException("Unknown request");
}
String query = urlAsString.substring("/image/source/".length());
boolean applyGrayscaleFilter = false;
int optionsIndex = query.lastIndexOf('?');
if (optionsIndex != -1) {
String urlOptionalParameters = query.substring(optionsIndex + 1);
query = query.substring(0, optionsIndex);
String filter = this.parseFilterParameter(urlOptionalParameters);
applyGrayscaleFilter = "grayscale".equals(filter);
}
String imageSource = URLDecoder.decode(urlAsString.substring("/image/source/".length()), "UTF-8");
// Load the image data and apply the grayscale filter if necessary
byte[] imageData = loadImageData(imageSource);
if (applyGrayscaleFilter) {
imageData = this.applyGrayscaleFilter(imageData);
}
// Send the image
response.setEntity(new EntityTemplate(new SimpleContentProducer(imageData)));
}
项目:opentravelmate
文件:ExtensionRequestHandler.java
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
String target = request.getRequestLine().getUri();
String url = URLDecoder.decode(target, "UTF-8");
if (url != null && url.startsWith("/")) {
url = url.substring(1);
}
byte[] content = IOUtils.toByteArray(assetManager.open(url));
EntityTemplate entity = new EntityTemplate(new SimpleContentProducer(content));
response.setEntity(entity);
}
项目:downloadclient
文件:DownloadStepConverter.java
private void unpagedWFSDownload(
JobList jl,
File workingDir,
Set<String> usedVars,
WFSMeta meta
) throws ConverterException {
WFSMeta.Operation getFeature = meta.findOperation(GETFEATURE);
boolean usePost = getFeature.getPOST() != null;
String url = usePost
? getFeature.getPOST()
: wfsURL(dls, usedVars, meta);
Document params = usePost
? WFSPostParamsBuilder.create(dls, usedVars, meta)
: null;
log.log(Level.INFO, () -> "url: " + url);
String ext = extension();
File gml = new File(workingDir, "download." + ext);
log.info(() -> "Download to file \"" + gml + "\"");
FileDownloadJob fdj = null;
if (usePost) {
HttpEntity ent = new EntityTemplate(
XML.toContentProducer(params));
fdj = new FileDownloadJob(
url, gml,
this.user, this.password,
ent,
this.logger);
} else {
fdj = new FileDownloadJob(
url, gml,
this.user, this.password,
this.logger);
}
jl.addJob(fdj);
if (ext.equals("gml")) {
jl.addJob(new GMLCheckJob(gml, logger));
jl.addJob(new BroadcastJob(I18n.getMsg("file.download.success")));
}
}
项目:downloadclient
文件:DownloadStepConverter.java
private int numFeatures(String wfsURL, Document postparams)
throws ConverterException {
URL url = postparams == null
? newURL(hitsURL(wfsURL))
: newURL(wfsURL);
Document hitsDoc = null;
try {
HttpEntity ent = postparams == null
? null
: new EntityTemplate(XML.toContentProducer(postparams));
hitsDoc = XML.getDocument(url, user, password, ent);
} catch (SocketTimeoutException | ConnectTimeoutException te) {
throw new ConverterException(
I18n.format(
"file.download.failed_reason",
I18n.getMsg("file.download.failed.timeout")),
te);
} catch (URISyntaxException | IOException e) {
throw new ConverterException(e.getMessage());
}
if (hitsDoc == null) {
throw new ConverterException(
I18n.getMsg("dls.converter.no.hits.doc"));
}
checkServiceException(hitsDoc);
String numberMatchedString = (String)XML.xpath(
hitsDoc, XPATH_NUMBER_MATCHED,
XPathConstants.STRING,
WFSMetaExtractor.NAMESPACES);
if (numberMatchedString == null || numberMatchedString.isEmpty()) {
throw new ConverterException(
I18n.getMsg("file.download.no.number"));
}
try {
return Integer.parseInt(numberMatchedString);
} catch (NumberFormatException nfe) {
throw new ConverterException(nfe.getLocalizedMessage(), nfe);
}
}
项目:fusion-client-tools
文件:FusionPipelineClient.java
public void postDocsToPipeline(String hostAndPort, String pipelinePath, List docs, int requestId, String contentType) throws Exception {
FusionSession fusionSession = getSession(hostAndPort, requestId);
String postUrl = hostAndPort + pipelinePath;
if (postUrl.indexOf("?") != -1) {
postUrl += "&echo=false";
} else {
postUrl += "?echo=false";
}
HttpPost postRequest = new HttpPost(postUrl);
ContentProducer cp = newContentProducer(contentType, docs);
EntityTemplate et = new EntityTemplate(cp);
et.setContentType(contentType);
et.setContentEncoding(StandardCharsets.UTF_8.name());
postRequest.setEntity(et);
HttpEntity entity = null;
CloseableHttpResponse response = null;
try {
HttpClientContext context = null;
if (isKerberos) {
response = httpClient.execute(postRequest);
} else {
context = HttpClientContext.create();
if (cookieStore != null) {
context.setCookieStore(cookieStore);
}
response = httpClient.execute(postRequest, context);
}
entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 401) {
// unauth'd - session probably expired? retry to establish
log.warn("Unauthorized error (401) when trying to send request " + requestId +
" to Fusion at " + hostAndPort + ", will re-try to establish session");
// re-establish the session and re-try the request
EntityUtils.consumeQuietly(entity);
entity = null;
response.close();
synchronized (this) {
fusionSession = resetSession(hostAndPort);
if (fusionSession == null)
throw new IllegalStateException("After re-establishing session when processing request " +
requestId + ", hostAndPort " + hostAndPort + " is no longer active! Try another hostAndPort.");
}
log.info("Going to re-try request " + requestId + " after session re-established with " + hostAndPort);
if (isKerberos) {
response = httpClient.execute(postRequest);
} else {
response = httpClient.execute(postRequest, context);
}
entity = response.getEntity();
statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 204) {
log.info("Re-try request " + requestId + " after session timeout succeeded for: " + hostAndPort);
} else {
raiseFusionServerException(hostAndPort, entity, statusCode, response, requestId);
}
} else if (statusCode != 200 && statusCode != 204) {
raiseFusionServerException(hostAndPort, entity, statusCode, response, requestId);
} else {
// OK!
if (fusionSession != null && fusionSession.docsSentMeter != null)
fusionSession.docsSentMeter.mark(docs.size());
}
} finally {
EntityUtils.consumeQuietly(entity);
if (response != null) response.close();
}
}
项目:tempto
文件:WebHdfsClient.java
@Override
public void saveFile(String path, RepeatableContentProducer repeatableContentProducer)
{
saveFile(path, new EntityTemplate(toApacheContentProducer(repeatableContentProducer)));
}
项目:Blackhole
文件:ListingHandler.java
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
String contentType = Utility.MIME_TYPES.get("html");
Integer code;
final byte[] r;
String resp;
AssetManager mgr = context.getAssets();
try {
resp = Utility.loadInputStreamAsString(mgr.open("index.html"));
code = HttpStatus.OK.getCode();
} catch (IOException e){
resp = Utility.loadInputStreamAsString(mgr.open("notfound.html"));
code = HttpStatus.NOT_FOUND.getCode();
}
final ArrayList<FileL> fl = new ArrayList<>();
Uri uri = Uri.parse(request.getRequestLine().getUri());
String folder="/";
if(uri.getPath().contains("~")) {
List<String> s = uri.getPathSegments();
String path = File.separator;
for(int i=1;i<s.size()-1;i++) {
path+=s.get(i)+File.separator;
}
if(s.size()>1) {
fl.add(new FileL("..", "/~" + path, "-", "DIR"));
folder = path + s.get(s.size() - 1) + File.separator;
}else{
folder = path;
}
}
File f = new File(Utility.BLACKHOLE_PATH +folder);
if(f.isDirectory()) {
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
fl.add(new FileL(inFile.getName(),"/~"+folder+inFile.getName(),"-","DIR"));
} else {
fl.add(new FileL(inFile.getName(),"/file"+folder+inFile.getName(),Utility.getFileSize(inFile)));
}
}
}
final String wfolder = folder;
Template tmpl = Mustache.compiler().compile(resp);
r=tmpl.execute(new Object() {
Object filelist = fl;
String title = context.getString(R.string.app_title);
String wd = wfolder;
}).getBytes();
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);
}
项目:marmotta
文件:ResourceClient.java
/**
* Update the content of the resource identified by the URI given as argument. The resource has to exist before
* content can be uploaded to it. Any existing content will be overridden. The stream of the content object
* will be consumed by this method. Throws ContentFormatException if the content type is not supported,
* NotFoundException if the resource does not exist.
* @param uri
* @param content
* @throws IOException
* @throws MarmottaClientException
*/
public void updateResourceContent(final String uri, final Content content) throws IOException, MarmottaClientException {
HttpClient httpClient = HTTPUtil.createClient(config);
HttpPut put = new HttpPut(getServiceUrl(uri));
put.setHeader(CONTENT_TYPE, content.getMimeType() + "; rel=content");
ContentProducer cp = new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
ByteStreams.copy(content.getStream(),outstream);
}
};
put.setEntity(new EntityTemplate(cp));
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
@Override
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
EntityUtils.consume(response.getEntity());
switch(response.getStatusLine().getStatusCode()) {
case 200:
log.debug("content for resource {} updated",uri);
return true;
case 406:
log.error("server does not support content type {} for resource {}, cannot update", content.getMimeType(),uri);
return false;
case 404:
log.error("resource {} does not exist, cannot update", uri);
return false;
default:
log.error("error updating resource {}: {} {}",new Object[] {uri,response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()});
return false;
}
}
};
try {
httpClient.execute(put, handler);
} catch(IOException ex) {
put.abort();
throw ex;
} finally {
put.releaseConnection();
}
}
项目:marmotta
文件:ConfigurationClient.java
/**
* Update the configuration "key" with the given value. Value can be either a list of values or one of the
* primitive types String, Boolean, Integer, Double
* @param key
* @param value
* @throws IOException
* @throws MarmottaClientException
*/
public void setConfiguration(String key, final Object value) throws IOException, MarmottaClientException {
HttpClient httpClient = HTTPUtil.createClient(config);
String serviceUrl = config.getMarmottaUri() + URL_CONFIG_SERVICE + "/data/" + URLEncoder.encode(key,"utf-8");
HttpPost post = new HttpPost(serviceUrl);
post.setHeader(CONTENT_TYPE, "application/json");
ContentProducer cp = new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
ObjectMapper mapper = new ObjectMapper();
if(value instanceof Collection) {
mapper.writeValue(outstream,value);
} else {
mapper.writeValue(outstream, Collections.singletonList(value.toString()));
}
}
};
post.setEntity(new EntityTemplate(cp));
try {
HttpResponse response = httpClient.execute(post);
switch(response.getStatusLine().getStatusCode()) {
case 200:
log.debug("configuration {} updated successfully",key);
break;
case 404:
log.error("configuration with key {} does not exist",key);
throw new NotFoundException("configuration with key "+key+" does not exist");
default:
log.error("error updating configuration {}: {} {}",new Object[] {key,response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()});
throw new MarmottaClientException("error updating configuration "+key+": "+response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
}
} finally {
post.releaseConnection();
}
}
项目:cim
文件:HttpDownHandler.java
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException {
if (!Config.ALLOW_DOWNLOAD) {
response.setStatusCode(HttpStatus.SC_SERVICE_UNAVAILABLE);
response.setEntity(ViewFactory.getSingleton().renderTemp(request, "503.html"));
return;
}
HttpGetParser parser = new HttpGetParser();
Map<String, String> params = parser.parse(request);
String fname = params.get("fname");
Header referer = request.getFirstHeader("Referer");
if (fname == null || referer == null) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
return;
}
fname = URLDecoder.decode(fname, Config.ENCODING);
String refPath = new URL(URLDecoder.decode(referer.getValue(), Config.ENCODING)).getPath();
final File file;
if (refPath.equals("/")) {
file = new File(this.webRoot, fname);
} else if (!refPath.startsWith(this.webRoot)) {
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
response.setEntity(ViewFactory.getSingleton().renderTemp(request, "403.html"));
return;
} else {
file = new File(refPath, fname);
}
final String encoding = isGBKAccepted(request) ? "GBK" : Config.ENCODING;
HttpEntity entity = new EntityTemplate(new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
if (file.isFile()) {
write(file, outstream);
} else {
zip(file, outstream, encoding);
}
}
});
response.setStatusCode(HttpStatus.SC_OK);
response.addHeader("Content-Description", "File Transfer");
response.setHeader("Content-Type", "application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + encodeFilename(file));
response.setHeader("Content-Transfer-Encoding", "binary");
// 在某平板自带浏览器上下载失败,比较下能成功下载的响应头,这里少了Content-Length。但设了,反而下不了了。
response.setEntity(entity);
}
项目:AndroidWebServ
文件:HttpDownHandler.java
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException {
if (!Config.ALLOW_DOWNLOAD) {
response.setStatusCode(HttpStatus.SC_SERVICE_UNAVAILABLE);
response.setEntity(ViewFactory.getSingleton().renderTemp(request, "503.html"));
return;
}
HttpGetParser parser = new HttpGetParser();
Map<String, String> params = parser.parse(request);
String fname = params.get("fname");
Header referer = request.getFirstHeader("Referer");
if (fname == null || referer == null) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
return;
}
fname = URLDecoder.decode(fname, Config.ENCODING);
String refPath = new URL(URLDecoder.decode(referer.getValue(), Config.ENCODING)).getPath();
final File file;
if (refPath.equals("/")) {
file = new File(this.webRoot, fname);
} else if (!refPath.startsWith(this.webRoot)) {
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
response.setEntity(ViewFactory.getSingleton().renderTemp(request, "403.html"));
return;
} else {
file = new File(refPath, fname);
}
final String encoding = isGBKAccepted(request) ? "GBK" : Config.ENCODING;
HttpEntity entity = new EntityTemplate(new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
if (file.isFile()) {
write(file, outstream);
} else {
zip(file, outstream, encoding);
}
}
});
response.setStatusCode(HttpStatus.SC_OK);
response.addHeader("Content-Description", "File Transfer");
response.setHeader("Content-Type", "application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + encodeFilename(file));
response.setHeader("Content-Transfer-Encoding", "binary");
// 在某平板自带浏览器上下载失败,比较下能成功下载的响应头,这里少了Content-Length。但设了,反而下不了了。
response.setEntity(entity);
}