本文整理汇总了Java中org.apache.http.entity.ByteArrayEntity.setContentType方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayEntity.setContentType方法的具体用法?Java ByteArrayEntity.setContentType怎么用?Java ByteArrayEntity.setContentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.ByteArrayEntity
的用法示例。
在下文中一共展示了ByteArrayEntity.setContentType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildEntity
import org.apache.http.entity.ByteArrayEntity; //导入方法依赖的package包/类
/**
* Build the HttpEntity to be sent to the Service as part of (POST) request. Creates a off-memory
* {@link FileExposingFileEntity} or a regular in-memory {@link ByteArrayEntity} depending on if the request
* OutputStream fit into memory when built by calling.
*
* @param request -
* @return - the built HttpEntity
* @throws IOException -
*/
protected HttpEntity buildEntity(final ClientInvocation request) throws IOException {
HttpEntity entityToBuild = null;
DeferredFileOutputStream memoryManagedOutStream = writeRequestBodyToOutputStream(request);
if (memoryManagedOutStream.isInMemory()) {
ByteArrayEntity entityToBuildByteArray =
new ByteArrayEntity(memoryManagedOutStream.getData());
entityToBuildByteArray.setContentType(
new BasicHeader(HTTP.CONTENT_TYPE, request.getHeaders().getMediaType().toString()));
entityToBuild = entityToBuildByteArray;
} else {
File requestBodyFile = memoryManagedOutStream.getFile();
requestBodyFile.deleteOnExit();
entityToBuild = new FileExposingFileEntity(
memoryManagedOutStream.getFile(), request.getHeaders().getMediaType().toString());
}
return entityToBuild;
}
示例2: handleException
import org.apache.http.entity.ByteArrayEntity; //导入方法依赖的package包/类
/**
* Handles the given exception and generates an HTTP response to be sent
* back to the client to inform about the exceptional condition encountered
* in the course of the request processing.
*
* @param ex the exception.
* @param response the HTTP response.
*/
protected void handleException(final HttpException ex, final HttpResponse response) {
if (ex instanceof MethodNotSupportedException) {
response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
} else if (ex instanceof UnsupportedHttpVersionException) {
response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
} else if (ex instanceof ProtocolException) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
} else {
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
String message = ex.getMessage();
if (message == null) {
message = ex.toString();
}
byte[] msg = EncodingUtils.getAsciiBytes(message);
ByteArrayEntity entity = new ByteArrayEntity(msg);
entity.setContentType("text/plain; charset=US-ASCII");
response.setEntity(entity);
}
示例3: HttpRequestWrapper
import org.apache.http.entity.ByteArrayEntity; //导入方法依赖的package包/类
public HttpRequestWrapper(HttpRequestBase originalRequest)
throws IllegalStateException, IOException {
this.originalRequest = originalRequest;
HttpEntity entity = null;
if (originalRequest instanceof HttpEntityEnclosingRequest
&& (entity = ((HttpEntityEnclosingRequest) originalRequest)
.getEntity()) != null) {
body = IOUtils.toByteArray(entity.getContent());
this.contentType = entity.getContentType() == null ? "" : entity
.getContentType().getValue();
this.contentLength = String.valueOf(body.length);
ByteArrayEntity newEntity = new ByteArrayEntity(body);
newEntity.setContentType(entity.getContentType());
((HttpEntityEnclosingRequest) originalRequest).setEntity(newEntity);
} else {
body = new byte[0];
contentType = "";
contentLength = "";
}
}
示例4: returnResponse
import org.apache.http.entity.ByteArrayEntity; //导入方法依赖的package包/类
public HttpResponse returnResponse() throws IOException {
assertNotConsumed();
try {
final HttpEntity entity = this.response.getEntity();
if (entity != null) {
final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
EntityUtils.toByteArray(entity));
final ContentType contentType = ContentType.getOrDefault(entity);
byteArrayEntity.setContentType(contentType.toString());
this.response.setEntity(byteArrayEntity);
}
return this.response;
} finally {
this.consumed = true;
}
}
示例5: addEntity
import org.apache.http.entity.ByteArrayEntity; //导入方法依赖的package包/类
private void addEntity ( HttpEntityEnclosingRequestBase req, String contentType, byte[] o )
{
final ByteArrayEntity input = new ByteArrayEntity ( o );
input.setContentType ( contentType );
req.addHeader ( "Content-Type", contentType );
req.setEntity ( input );
}
示例6: handle
import org.apache.http.entity.ByteArrayEntity; //导入方法依赖的package包/类
/**
* Handles a request by echoing the incoming request entity.
* If there is no request entity, an empty document is returned.
*
* @param request the request
* @param response the response
* @param context the context
*
* @throws HttpException in case of a problem
* @throws IOException in case of an IO problem
*/
@Override
public void handle(final HttpRequest request,
final HttpResponse response,
final HttpContext context)
throws HttpException, IOException {
final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ROOT);
if (!"GET".equals(method) &&
!"POST".equals(method) &&
!"PUT".equals(method)
) {
throw new MethodNotSupportedException
(method + " not supported by " + getClass().getName());
}
HttpEntity entity = null;
if (request instanceof HttpEntityEnclosingRequest) {
entity = ((HttpEntityEnclosingRequest)request).getEntity();
}
// For some reason, just putting the incoming entity into
// the response will not work. We have to buffer the message.
byte[] data;
if (entity == null) {
data = new byte [0];
} else {
data = EntityUtils.toByteArray(entity);
}
final ByteArrayEntity bae = new ByteArrayEntity(data);
if (entity != null) {
bae.setContentType(entity.getContentType());
}
entity = bae;
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(entity);
}
示例7: setRequestBody
import org.apache.http.entity.ByteArrayEntity; //导入方法依赖的package包/类
/**
* Set the given serialized remote invocation as request body.
* <p>The default implementation simply sets the serialized invocation as the
* HttpPost's request body. This can be overridden, for example, to write a
* specific encoding and to potentially set appropriate HTTP request headers.
* @param config the HTTP invoker configuration that specifies the target service
* @param httpPost the HttpPost to set the request body on
* @param baos the ByteArrayOutputStream that contains the serialized
* RemoteInvocation object
* @throws java.io.IOException if thrown by I/O methods
*/
protected void setRequestBody(
HttpInvokerClientConfiguration config, HttpPost httpPost, ByteArrayOutputStream baos)
throws IOException {
ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray());
entity.setContentType(getContentType());
httpPost.setEntity(entity);
}