当前位置: 首页>>代码示例>>Java>>正文


Java ContainerRequestContext.getEntityStream方法代码示例

本文整理汇总了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;
}
 
开发者ID:code-obos,项目名称:servicebuilder,代码行数:20,代码来源:ServerLogFilter.java

示例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();
}
 
开发者ID:dotwebstack,项目名称:dotwebstack-framework,代码行数:29,代码来源:RequestParameterExtractor.java

示例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();
}
 
开发者ID:Erudika,项目名称:para-search-elasticsearch,代码行数:36,代码来源:ProxyResourceHandler.java

示例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;
}
 
开发者ID:groovylabs,项目名称:lyre,代码行数:30,代码来源:EndpointUtils.java


注:本文中的javax.ws.rs.container.ContainerRequestContext.getEntityStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。