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; }
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(); } } } } } } }
protected void decodeAttributes(HttpPostRequestDecoder decoder, NettyHttpRequest request) throws IOException { try { while (decoder.hasNext()) { InterfaceHttpData interfaceHttpData = decoder.next(); if (interfaceHttpData.getHttpDataType() == HttpDataType.Attribute) { Attribute attribute = (Attribute) interfaceHttpData; String name = attribute.getName(); String value = attribute.getValue(); List<String> attrValues = request.getParameters().get(name); if (attrValues == null) { attrValues = new LinkedList<String>(); request.getParameters().put(name, attrValues); } attrValues.add(value); } else if (interfaceHttpData.getHttpDataType() == HttpDataType.FileUpload) { FileUpload fileUpload = (FileUpload) interfaceHttpData; request.getUploadedFiles().add(fileUpload); } } } catch (HttpPostRequestDecoder.EndOfDataDecoderException e) { log.debug("{} occured: {}", "EndOfDataDecoderException", e); } }
private void decodePost() { HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request); // Form Data List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas(); try { for (InterfaceHttpData data : bodyHttpDatas) { if (data.getHttpDataType().equals(HttpDataType.Attribute)) { Attribute attr = (MixedAttribute) data; params.put(data.getName(), new Param(Arrays.asList(attr.getValue()))); } } } catch (IOException e) { throw new RuntimeException(e); } decoder.destroy(); }
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; }
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); } }
private String domainKey(FullHttpRequest request) throws IOException { FullHttpRequest copy = request.copy(); ReferenceCountUtil.releaseLater(copy); HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(copy); List<InterfaceHttpData> keyDatas = decoder.getBodyHttpDatas("domain_key"); assertThat(keyDatas, is(not(nullValue()))); assertThat(keyDatas.size(), is(1)); InterfaceHttpData domainKeyData = keyDatas.get(0); assertThat(domainKeyData.getHttpDataType(), is(HttpDataType.Attribute)); return ((Attribute) domainKeyData).getValue(); }
public GaleRequest process(FullHttpRequest request) { GaleRequest result = new GaleRequest(); result.setUri(request.getUri()); result.setMethod(request.getMethod()); result.setHeaders(request.headers()); result.setVersion(request.getProtocolVersion()); //parse query parameters QueryStringDecoder queryDecoder = new QueryStringDecoder(request.getUri(), CharsetUtil.UTF_8); result.setPath(queryDecoder.path()); result.getParameters().putAll(queryDecoder.parameters()); //parse body parameters HttpPostRequestDecoder bodyDecoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(true), request); List<InterfaceHttpData> datum = bodyDecoder.getBodyHttpDatas(); if (datum != null && !datum.isEmpty()) { for (InterfaceHttpData data : datum) { String name = data.getName(); String value = null; if (data.getHttpDataType().equals(HttpDataType.Attribute)) { //do not parse file data Attribute attribute = (Attribute)data; try { value = attribute.getString(CharsetUtil.UTF_8); result.getParameters().add(name, value); } catch(Exception e) { ELOGGER.error(this.getClass().getName(), e); } } } } bodyDecoder.destroy(); return result; }
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; }
@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"); } } }