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; }
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(); } }
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(); } } } } } } }
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); } } }
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(); }
@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; }
private String toString(InterfaceHttpData p) { return callUnchecked(() -> { if (p != null) { if (p instanceof FileUpload) { return FileUpload.class.cast(p).getFilename(); } if (p instanceof Attribute) { return Attribute.class.cast(p).getValue(); } else { return null; } } else { return null; } }); }
public void setHttpRequest(HttpRequest request) throws BadRequestException, IOException { if (request == null) { LOG.error("HttpRequest not initialized"); throw new BadRequestException("HttpRequest not initialized"); } if (!request.getMethod().equals(HttpMethod.POST)) { LOG.error("Got invalid HTTP method: expecting only POST"); throw new BadRequestException("Incorrect method " + request.getMethod().toString() + ", expected POST"); } HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(factory, request); InterfaceHttpData data = decoder.getBodyHttpData(HTTP_TEST_ATTRIBUTE); if (data == null) { LOG.error("HTTP Resolve request inccorect, {} attribute not found", HTTP_TEST_ATTRIBUTE); throw new BadRequestException("HTTP Resolve request inccorect, " + HTTP_TEST_ATTRIBUTE + " attribute not found"); } Attribute attribute = (Attribute) data; requestData = attribute.get(); LOG.trace("Name {}, type {} found, data size {}", data.getName(), data.getHttpDataType().name(), requestData.length); }
private Map<String, List<String>> parseQueryParams() { // query string final QueryStringDecoder query = new QueryStringDecoder(uri()); final Map<String, List<String>> queryParams = new HashMap<>(query.parameters()); //TODO multipart/form-data if (!"application/x-www-form-urlencoded".equalsIgnoreCase(contentType())) return queryParams; // http body final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request); final List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas(); bodyHttpDatas.stream() .parallel() .filter(e -> e.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) .map(e -> (Attribute) e) .map(e -> { try { return new AbstractMap.SimpleImmutableEntry<String, String>(e.getName(), e.getValue()); } catch (IOException ex) { throw new RuntimeException(ex); } }) .forEach(e -> { String key = e.getKey(); if (!queryParams.containsKey(key)) queryParams.putIfAbsent(key, new ArrayList<>(1)); queryParams.get(key).add(e.getValue()); }); return queryParams; }
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; }
/** * Add a simple attribute in the body as Name=Value * * @param name * name of the parameter * @param value * the value of the parameter * @throws NullPointerException * for name * @throws ErrorDataEncoderException * if the encoding is in error or if the finalize were already done */ public void addBodyAttribute(String name, String value) throws ErrorDataEncoderException { if (name == null) { throw new NullPointerException("name"); } String svalue = value; if (value == null) { svalue = ""; } Attribute data = factory.createAttribute(request, name, svalue); addBodyHttpData(data); }
private void addToParameters(Map<String, List<String>> parameters, Attribute attribute) { try { String value = attribute.getValue(); if (Strings.isNullOrEmpty(value)) { return; } String name = attribute.getName(); List<String> params = parameters.computeIfAbsent(name, k -> new ArrayList<>()); params.add(value); } catch (Exception e) { throw new RequestProcessingException(e.getMessage(), e); } }
@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); }
@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"); } } }