本文整理汇总了Java中io.netty.handler.codec.http.multipart.InterfaceHttpData.getHttpDataType方法的典型用法代码示例。如果您正苦于以下问题:Java InterfaceHttpData.getHttpDataType方法的具体用法?Java InterfaceHttpData.getHttpDataType怎么用?Java InterfaceHttpData.getHttpDataType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.multipart.InterfaceHttpData
的用法示例。
在下文中一共展示了InterfaceHttpData.getHttpDataType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPostParameter
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
public static String getPostParameter(HttpRequest req, String name) {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(
new DefaultHttpDataFactory(false), req);
InterfaceHttpData data = decoder.getBodyHttpData(name);
if (data.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
String value = null;
try {
value = attribute.getValue();
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
return null;
}
示例2: parsePostParam
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
private void parsePostParam(InterfaceHttpData data) {
try {
switch (data.getHttpDataType()) {
case Attribute:
Attribute param = (Attribute) data;
this.paramsMap.put(param.getName(), Collections.singletonList(param.getValue()));
break;
default:
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally {
data.release();
}
}
示例3: copyHttpBodyData
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
void copyHttpBodyData(FullHttpRequest fullHttpReq, MockHttpServletRequest servletRequest){
ByteBuf bbContent = fullHttpReq.content();
if(bbContent.hasArray()) {
servletRequest.setContent(bbContent.array());
} else {
if(fullHttpReq.getMethod().equals(HttpMethod.POST)){
HttpPostRequestDecoder decoderPostData = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), fullHttpReq);
String bbContentStr = bbContent.toString(Charset.forName(UTF_8));
servletRequest.setContent(bbContentStr.getBytes());
if( ! decoderPostData.isMultipart() ){
List<InterfaceHttpData> postDatas = decoderPostData.getBodyHttpDatas();
for (InterfaceHttpData postData : postDatas) {
if (postData.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute) postData;
try {
servletRequest.addParameter(attribute.getName(),attribute.getValue());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
示例4: writeHttpData
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
private void writeHttpData(InterfaceHttpData data) throws IOException
{
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute)
{
Attribute attribute = (Attribute) data;
String value = attribute.getValue();
if (value.length() > 65535)
{
throw new IOException("Data too long");
}
req.addPostParameter(attribute.getName(), value);
}
else
{
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload)
{
FileUpload fileUpload = (FileUpload) data;
req.addFileUpload(fileUpload);
}
}
}
示例5: get
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
@Override
public Object get(ChannelHandlerContext ctx, URIDecoder uriDecoder) {
List<InterfaceHttpData> bodyHttpDatas = uriDecoder.getBodyHttpDatas();
if (bodyHttpDatas == null || bodyHttpDatas.size() == 0) {
return null;
}
for (InterfaceHttpData data : bodyHttpDatas) {
if (name.equals(data.getName())) {
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
try {
return ReflectionUtil.castTo(type, attribute.getValue());
} catch (IOException e) {
log.error("Error getting form params. Reason : {}", e.getMessage(), e);
}
}
}
}
return null;
}
示例6: execute
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
@Override
public CompletableFuture<ResponseInfo<String>> execute(RequestInfo<String> request, Executor longRunningTaskExecutor, ChannelHandlerContext ctx) {
List<String> hashesFound = new ArrayList<>();
for (InterfaceHttpData multipartData : request.getMultipartParts()) {
String name = multipartData.getName();
byte[] payloadBytes;
try {
payloadBytes = ((HttpData)multipartData).get();
} catch (IOException e) {
throw new RuntimeException(e);
}
String filename = null;
switch (multipartData.getHttpDataType()) {
case Attribute:
// Do nothing - filename stays null
break;
case FileUpload:
filename = ((FileUpload)multipartData).getFilename();
break;
default:
throw new RuntimeException("Unsupported multipart type: " + multipartData.getHttpDataType().name());
}
hashesFound.add(getHashForMultipartPayload(name, filename, payloadBytes));
}
return CompletableFuture.completedFuture(ResponseInfo.newBuilder(StringUtils.join(hashesFound, ",")).build());
}
示例7: readFileUploadData
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
private HttpResponseStatus readFileUploadData() throws IOException {
while (decoder.hasNext()) {
final InterfaceHttpData data = decoder.next();
if (data != null) {
try {
logger.info("BODY FileUpload: " + data.getHttpDataType().name() + ": " + data);
if (data.getHttpDataType() == HttpDataType.FileUpload) {
final FileUpload fileUpload = (FileUpload) data;
if (fileUpload.isCompleted()) {
requestProcessed = true;
final String format = ImageStoreUtil.checkTemplateFormat(fileUpload.getFile().getAbsolutePath(), fileUpload.getFilename());
if (StringUtils.isNotBlank(format)) {
final String errorString = "File type mismatch between the sent file and the actual content. Received: " + format;
logger.error(errorString);
responseContent.append(errorString);
storageResource.updateStateMapWithError(uuid, errorString);
return HttpResponseStatus.BAD_REQUEST;
}
final String status = storageResource.postUpload(uuid, fileUpload.getFile().getName());
if (status != null) {
responseContent.append(status);
storageResource.updateStateMapWithError(uuid, status);
return HttpResponseStatus.INTERNAL_SERVER_ERROR;
} else {
responseContent.append("upload successful.");
return HttpResponseStatus.OK;
}
}
}
} finally {
data.release();
}
}
}
responseContent.append("received entity is not a file");
return HttpResponseStatus.UNPROCESSABLE_ENTITY;
}
示例8: handleUploadFile
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
private void handleUploadFile(InterfaceHttpData data, Message uploadMessage) throws IOException{
FileForm fileForm = uploadMessage.fileForm;
if(uploadMessage.fileForm == null){
uploadMessage.fileForm = fileForm = new FileForm();
}
if (data.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
fileForm.attributes.put(attribute.getName(), attribute.getValue());
return;
}
if (data.getHttpDataType() == HttpDataType.FileUpload) {
FileUpload fileUpload = (FileUpload) data;
Message.FileUpload file = new Message.FileUpload();
file.fileName = fileUpload.getFilename();
file.contentType = fileUpload.getContentType();
file.data = fileUpload.get();
List<Message.FileUpload> uploads = fileForm.files.get(data.getName());
if(uploads == null){
uploads = new ArrayList<Message.FileUpload>();
fileForm.files.put(data.getName(), uploads);
}
uploads.add(file);
}
}
示例9: readFileUploadData
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
private HttpResponseStatus readFileUploadData() throws IOException {
while (decoder.hasNext()) {
InterfaceHttpData data = decoder.next();
if (data != null) {
try {
logger.info("BODY FileUpload: " + data.getHttpDataType().name() + ": " + data);
if (data.getHttpDataType() == HttpDataType.FileUpload) {
FileUpload fileUpload = (FileUpload) data;
if (fileUpload.isCompleted()) {
requestProcessed = true;
String format = ImageStoreUtil.checkTemplateFormat(fileUpload.getFile().getAbsolutePath(), fileUpload.getFilename());
if(StringUtils.isNotBlank(format)) {
String errorString = "File type mismatch between the sent file and the actual content. Received: " + format;
logger.error(errorString);
responseContent.append(errorString);
storageResource.updateStateMapWithError(uuid, errorString);
return HttpResponseStatus.BAD_REQUEST;
}
String status = storageResource.postUpload(uuid, fileUpload.getFile().getName());
if (status != null) {
responseContent.append(status);
storageResource.updateStateMapWithError(uuid, status);
return HttpResponseStatus.INTERNAL_SERVER_ERROR;
} else {
responseContent.append("upload successful.");
return HttpResponseStatus.OK;
}
}
}
} finally {
data.release();
}
}
}
responseContent.append("received entity is not a file");
return HttpResponseStatus.UNPROCESSABLE_ENTITY;
}
示例10: messageReceived
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
@Override
public void messageReceived(ChannelHandlerContext ctx, Env env) throws Exception {
if (env.getRequest().getMethod() == POST || env.getRequest().getMethod() == PUT || env.getRequest().getMethod() == PATCH) {
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(env.getRequest());
for (InterfaceHttpData entry: decoder.getBodyHttpDatas()) {
if (entry.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute)entry;
env.getRequest().setParam((String)attribute.getName(), (String)attribute.getValue());
}
}
}
nextHandler(ctx, env);
}
示例11: parse
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
@Override
public void parse() throws Exception {
LOG.trace("CommandName: " + COMMAND_NAME + ": Parse..");
HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(factory, getRequest());
if (decoder.isMultipart()) {
LOG.trace("Chunked: " + HttpHeaders.isTransferEncodingChunked(getRequest()));
LOG.trace(": Multipart..");
List<InterfaceHttpData> datas = decoder.getBodyHttpDatas();
if (!datas.isEmpty()) {
for (InterfaceHttpData data : datas) {
LOG.trace("Multipart1 name " + data.getName() + " type " + data.getHttpDataType().name());
if (data.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
if (CommonEpConstans.REQUEST_SIGNATURE_ATTR_NAME.equals(data.getName())) {
requestSignature = attribute.get();
if (LOG.isTraceEnabled()) {
LOG.trace("Multipart name " + data.getName() + " type "
+ data.getHttpDataType().name() + " Signature set. size: "
+ requestSignature.length);
LOG.trace(MessageEncoderDecoder.bytesToHex(requestSignature));
}
} else if (CommonEpConstans.REQUEST_KEY_ATTR_NAME.equals(data.getName())) {
requestKey = attribute.get();
if (LOG.isTraceEnabled()) {
LOG.trace("Multipart name " + data.getName() + " type "
+ data.getHttpDataType().name() + " requestKey set. size: "
+ requestKey.length);
LOG.trace(MessageEncoderDecoder.bytesToHex(requestKey));
}
} else if (CommonEpConstans.REQUEST_DATA_ATTR_NAME.equals(data.getName())) {
requestData = attribute.get();
if (LOG.isTraceEnabled()) {
LOG.trace("Multipart name " + data.getName() + " type "
+ data.getHttpDataType().name() + " requestData set. size: "
+ requestData.length);
LOG.trace(MessageEncoderDecoder.bytesToHex(requestData));
}
} else if (CommonEpConstans.NEXT_PROTOCOL_ATTR_NAME.equals(data.getName())) {
nextProtocol = ByteBuffer.wrap(attribute.get()).getInt();
LOG.trace("[{}] next protocol is {}", getSessionUuid(), nextProtocol);
}
}
}
} else {
LOG.error("Multipart.. size 0");
throw new BadRequestException("HTTP Request inccorect, multiprat size is 0");
}
}
}
示例12: processPart
import io.netty.handler.codec.http.multipart.InterfaceHttpData; //导入方法依赖的package包/类
/**
* Processes a single decoded part in a multipart request. Exposes the data in the part either through the channel
* itself (if it is the blob part) or via {@link #getArgs()}.
* @param part the {@link InterfaceHttpData} that needs to be processed.
* @throws RestServiceException if the request channel is closed, if there is more than one part of the same name, if
* the size obtained from the headers does not match the actual size of the blob part or
* if {@code part} is not of the expected type ({@link FileUpload}).
*/
private void processPart(InterfaceHttpData part) throws RestServiceException {
if (part.getHttpDataType() == InterfaceHttpData.HttpDataType.FileUpload) {
FileUpload fileUpload = (FileUpload) part;
if (fileUpload.getName().equals(RestUtils.MultipartPost.BLOB_PART)) {
// this is actual data.
if (hasBlob) {
nettyMetrics.repeatedPartsError.inc();
throw new RestServiceException("Request has more than one " + RestUtils.MultipartPost.BLOB_PART,
RestServiceErrorCode.BadRequest);
} else {
hasBlob = true;
if (getSize() != -1 && fileUpload.length() != getSize()) {
nettyMetrics.multipartRequestSizeMismatchError.inc();
throw new RestServiceException(
"Request size [" + fileUpload.length() + "] does not match Content-Length [" + getSize() + "]",
RestServiceErrorCode.BadRequest);
} else {
contentLock.lock();
try {
if (isOpen()) {
requestContents.add(new DefaultHttpContent(ReferenceCountUtil.retain(fileUpload.content())));
} else {
nettyMetrics.multipartRequestAlreadyClosedError.inc();
throw new RestServiceException("Request is closed", RestServiceErrorCode.RequestChannelClosed);
}
} finally {
contentLock.unlock();
}
}
}
} else {
// this is any kind of data. (For ambry, this will be user metadata).
// TODO: find a configurable way of rejecting unexpected file parts.
String name = fileUpload.getName();
if (allArgs.containsKey(name)) {
nettyMetrics.repeatedPartsError.inc();
throw new RestServiceException("Request already has a component named " + name,
RestServiceErrorCode.BadRequest);
} else {
ByteBuffer buffer = ByteBuffer.allocate(fileUpload.content().readableBytes());
// TODO: Possible optimization - Upgrade ByteBufferReadableStreamChannel to take a list of ByteBuffer. This
// TODO: will avoid the copy.
fileUpload.content().readBytes(buffer);
buffer.flip();
allArgs.put(name, buffer);
}
}
} else {
nettyMetrics.unsupportedPartError.inc();
throw new RestServiceException("Unexpected HTTP data", RestServiceErrorCode.BadRequest);
}
}