本文整理汇总了Java中javax.ws.rs.container.ContainerRequestContext.setEntityStream方法的典型用法代码示例。如果您正苦于以下问题:Java ContainerRequestContext.setEntityStream方法的具体用法?Java ContainerRequestContext.setEntityStream怎么用?Java ContainerRequestContext.setEntityStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.ws.rs.container.ContainerRequestContext
的用法示例。
在下文中一共展示了ContainerRequestContext.setEntityStream方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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;
}
示例3: getParametersMap
import javax.ws.rs.container.ContainerRequestContext; //导入方法依赖的package包/类
/**
* 获取请求参数
* @param request
* @param inputStream
* @return
* @throws IOException
*/
public static Map<String, Object> getParametersMap(ContainerRequestContext requestContext,
HttpServletRequest request) throws IOException {
if (isMultipartContent(request)) {
return buildQueryParamsMap(request);
}
Map<String, Object> parameters = buildQueryParamsMap(request);
if (RestConst.GET_METHOD.equals(request.getMethod())) {
return parameters;
} else if (RestConst.POST_METHOD.equals(request.getMethod())) {
byte[] byteArray = IOUtils.toByteArray(request.getInputStream());
//reset InputStream
requestContext.setEntityStream(new ByteArrayInputStream(byteArray));
if (byteArray == null || byteArray.length == 0)
return parameters;
String content = new String(byteArray);
//JSON
// try {
// return JsonUtils.toObject(content, Map.class);
// } catch (Exception e) {}
if (content.contains("{")) {
content = StringUtils.left(content, 2048).trim();
if (!content.endsWith("}"))
content = content + "...\n}";
parameters.put("data", content);
return parameters;
}
String[] split = content.split("\\&");
for (String s : split) {
String[] split2 = s.split("=");
if (split2.length == 2 && StringUtils.isNotBlank(split2[1])) {
parameters.put(split2[0], split2[1]);
}
}
return parameters;
}
return null;
}