本文整理匯總了Java中javax.ws.rs.container.ContainerRequestContext.getEntityStream方法的典型用法代碼示例。如果您正苦於以下問題:Java ContainerRequestContext.getEntityStream方法的具體用法?Java ContainerRequestContext.getEntityStream怎麽用?Java ContainerRequestContext.getEntityStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.ws.rs.container.ContainerRequestContext
的用法示例。
在下文中一共展示了ContainerRequestContext.getEntityStream方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: extractRequestEntity
import javax.ws.rs.container.ContainerRequestContext; //導入方法依賴的package包/類
static String extractRequestEntity(ContainerRequestContext request) {
if (request.hasEntity()) {
InputStream inputStreamOriginal = request.getEntityStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamOriginal, MAX_ENTITY_READ);
bufferedInputStream.mark(MAX_ENTITY_READ);
byte[] bytes = new byte[MAX_ENTITY_READ];
int read;
try {
read = bufferedInputStream.read(bytes, 0, MAX_ENTITY_READ);
bufferedInputStream.reset();
} catch (IOException e) {
throw new RuntimeException(e);
}
request.setEntityStream(bufferedInputStream);
return new String(bytes, Charsets.UTF_8);
}
return null;
}
示例2: extractBody
import javax.ws.rs.container.ContainerRequestContext; //導入方法依賴的package包/類
/**
* Extracts body from a provided request context. Note that this method blocks until the body is
* not fully read. Also note that it would not be possible to read from the underlying stream
* again. {@link IOUtils#copy(InputStream, Writer)} is used.
*
* @param ctx JAX-RS request context
* @return body of the underlying stream or <code>null</code> if the provided request contains no
* body
* @throws IOException in case of any problems with the underlying stream
*/
@SuppressWarnings("resource")
@Nullable
private static String extractBody(ContainerRequestContext ctx) throws IOException {
/*
* do not use auto-closable construction as it is responsibility of JAX-RS to close the
* underlying stream
*/
IsEmptyCheckInputStream inputStream = new IsEmptyCheckInputStream(ctx.getEntityStream());
if (inputStream.isEmpty()) {
return null;
}
StringWriter writer = new StringWriter();
/* still copy from PushbackStream, not from the underlying stream */
IOUtils.copy(inputStream, writer);
return writer.toString();
}
示例3: proxyRequest
import javax.ws.rs.container.ContainerRequestContext; //導入方法依賴的package包/類
Response proxyRequest(String method, ContainerRequestContext ctx) {
if (!Config.getConfigBoolean("es.proxy_enabled", false)) {
return Response.status(Response.Status.FORBIDDEN.getStatusCode(), "This feature is disabled.").build();
}
String appid = ParaObjectUtils.getAppidFromAuthHeader(ctx.getHeaders().getFirst(HttpHeaders.AUTHORIZATION));
String path = getCleanPath(getPath(ctx));
if (StringUtils.isBlank(appid)) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
try {
if ("reindex".equals(path) && POST.equals(method)) {
return handleReindexTask(appid);
}
Header[] headers = getHeaders(ctx.getHeaders());
HttpEntity resp;
RestClient client = getClient(appid);
if (client != null) {
if (ctx.getEntityStream() != null && ctx.getEntityStream().available() > 0) {
HttpEntity body = new InputStreamEntity(ctx.getEntityStream(), ContentType.APPLICATION_JSON);
resp = client.performRequest(method, path, Collections.emptyMap(), body, headers).getEntity();
} else {
resp = client.performRequest(method, path, headers).getEntity();
}
if (resp != null && resp.getContent() != null) {
Header type = resp.getContentType();
Object response = getTransformedResponse(appid, resp.getContent(), ctx);
return Response.ok(response).header(type.getName(), type.getValue()).build();
}
}
} catch (Exception ex) {
logger.warn("Failed to proxy '{} {}' to Elasticsearch: {}", method, path, ex.getMessage());
}
return Response.status(Response.Status.BAD_REQUEST).build();
}
示例4: getEntityBody
import javax.ws.rs.container.ContainerRequestContext; //導入方法依賴的package包/類
/**
* Return the body json in one String Object.
*/
public String getEntityBody(ContainerRequestContext requestContext) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = requestContext.getEntityStream();
String result = null;
try {
ReaderWriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
if (requestEntity.length == 0) {
result = "";
} else {
result = new String(requestEntity, "UTF-8");
}
requestContext.setEntityStream(new ByteArrayInputStream(requestEntity));
} catch (IOException e) {
LOGGER.error("Error to read the body of this request.");
if (lyreProperties.isDebug()) {
LOGGER.debug("Stacktrace", e);
} else
LOGGER.warn("\u21B3 " + "Enable debug mode to see stacktrace log");
}
return result;
}