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(); } }
/** * request parameters put in {@link #parameters} * * @param req */ private void requestParametersHandler(HttpRequest req) { if (req.method().equals(HttpMethod.POST)) { HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req); try { List<InterfaceHttpData> postList = decoder.getBodyHttpDatas(); for (InterfaceHttpData data : postList) { List<String> values = new ArrayList<String>(); MixedAttribute value = (MixedAttribute) data; value.setCharset(CharsetUtil.UTF_8); values.add(value.getValue()); this.parameters.put(data.getName(), values); } } catch (Exception e) { logger.error(e.getMessage()); } } }
/** * {@inheritDoc} */ @Override public synchronized List<InterfaceHttpData> getMultipartParts() { if (!isMultipartRequest() || !isCompleteRequestWithAllChunks()) return null; if (multipartData == null) { byte[] contentBytes = getRawContentBytes(); HttpRequest fullHttpRequestForMultipartDecoder = (contentBytes == null) ? new DefaultFullHttpRequest(getProtocolVersion(), getMethod(), getUri()) : new DefaultFullHttpRequest(getProtocolVersion(), getMethod(), getUri(), Unpooled.wrappedBuffer(contentBytes)); fullHttpRequestForMultipartDecoder.headers().add(getHeaders()); multipartData = new HttpPostMultipartRequestDecoder( new DefaultHttpDataFactory(false), fullHttpRequestForMultipartDecoder, getContentCharset() ); } return multipartData.getBodyHttpDatas(); }
@Test public void getMultipartParts_works_as_expected_with_known_valid_data() throws IOException { // given RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); Whitebox.setInternalState(requestInfo, "isMultipart", true); Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8); Whitebox.setInternalState(requestInfo, "protocolVersion", HttpVersion.HTTP_1_1); Whitebox.setInternalState(requestInfo, "method", HttpMethod.POST); requestInfo.isCompleteRequestWithAllChunks = true; requestInfo.rawContentBytes = KNOWN_MULTIPART_DATA_BODY.getBytes(CharsetUtil.UTF_8); requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER); // when List<InterfaceHttpData> result = requestInfo.getMultipartParts(); // then assertThat(result, notNullValue()); assertThat(result.size(), is(1)); InterfaceHttpData data = result.get(0); assertThat(data, instanceOf(FileUpload.class)); FileUpload fileUploadData = (FileUpload)data; assertThat(fileUploadData.getName(), is(KNOWN_MULTIPART_DATA_NAME)); assertThat(fileUploadData.getFilename(), is(KNOWN_MULTIPART_DATA_FILENAME)); assertThat(fileUploadData.getString(CharsetUtil.UTF_8), is(KNOWN_MULTIPART_DATA_ATTR_UUID)); }
@Test public void getMultipartParts_works_as_expected_with_known_empty_data() throws IOException { // given RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); Whitebox.setInternalState(requestInfo, "isMultipart", true); Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8); Whitebox.setInternalState(requestInfo, "protocolVersion", HttpVersion.HTTP_1_1); Whitebox.setInternalState(requestInfo, "method", HttpMethod.POST); requestInfo.isCompleteRequestWithAllChunks = true; requestInfo.rawContentBytes = null; requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER); // when List<InterfaceHttpData> result = requestInfo.getMultipartParts(); // then assertThat(result, notNullValue()); assertThat(result.isEmpty(), is(true)); }
@Test(expected = IllegalStateException.class) public void getMultipartParts_explodes_if_multipartData_had_been_released() throws IOException { // given RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); Whitebox.setInternalState(requestInfo, "isMultipart", true); Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8); Whitebox.setInternalState(requestInfo, "protocolVersion", HttpVersion.HTTP_1_1); Whitebox.setInternalState(requestInfo, "method", HttpMethod.POST); requestInfo.isCompleteRequestWithAllChunks = true; requestInfo.rawContentBytes = KNOWN_MULTIPART_DATA_BODY.getBytes(CharsetUtil.UTF_8); requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER); List<InterfaceHttpData> result = requestInfo.getMultipartParts(); assertThat(result, notNullValue()); assertThat(result.size(), is(1)); // expect requestInfo.releaseMultipartData(); requestInfo.getMultipartParts(); fail("Expected an error, but none was thrown"); }
@Test public void getMultipartParts_returns_data_from_multipartData() { // given RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); Whitebox.setInternalState(requestInfo, "isMultipart", true); requestInfo.isCompleteRequestWithAllChunks = true; HttpPostMultipartRequestDecoder multipartDataMock = mock(HttpPostMultipartRequestDecoder.class); List<InterfaceHttpData> dataListMock = mock(List.class); doReturn(dataListMock).when(multipartDataMock).getBodyHttpDatas(); requestInfo.multipartData = multipartDataMock; // when List<InterfaceHttpData> result = requestInfo.getMultipartParts(); // then assertThat(result, is(dataListMock)); }
private void handleUploadMessage(HttpMessage httpMsg, Message uploadMessage) throws IOException{ if (httpMsg instanceof HttpContent) { HttpContent chunk = (HttpContent) httpMsg; decoder.offer(chunk); try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data != null) { try { handleUploadFile(data, uploadMessage); } finally { data.release(); } } } } catch (EndOfDataDecoderException e1) { //ignore } if (chunk instanceof LastHttpContent) { resetUpload(); } } }
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 readHttpDataChunkByChunk() throws IOException { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data != null) { try { // new value writeHttpData(data); } finally { data.release(); } } } }
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); } } }
/** * Example of reading request by chunk and getting values from chunk to chunk */ private void readHttpDataChunkByChunk() { try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data != null) { try { // new value writeHttpData(data); } finally { data.release(); } } } } catch (EndOfDataDecoderException e1) { // end responseContent.append("\r\n\r\nEND OF CONTENT CHUNK BY CHUNK\r\n\r\n"); } }
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; } }); }
private void readHttpDataChunkByChunk() { int chunk=0; String checksum=""; int sum=0; try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data != null) { try { writeHttpData(data,chunk,checksum,sum); } finally { data.release(); } } } } catch (EndOfDataDecoderException e1) { return; } }
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; }
@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()); }
@Test public void getMultipartParts_returns_null_if_isMultipart_is_false() { // given RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); Whitebox.setInternalState(requestInfo, "isMultipart", false); requestInfo.isCompleteRequestWithAllChunks = true; // when List<InterfaceHttpData> result = requestInfo.getMultipartParts(); // then assertThat(result, nullValue()); }
@Test public void getMultipartParts_returns_null_if_isCompleteRequestWithAllChunks_is_false() { // given RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); Whitebox.setInternalState(requestInfo, "isMultipart", true); requestInfo.isCompleteRequestWithAllChunks = false; // when List<InterfaceHttpData> result = requestInfo.getMultipartParts(); // then assertThat(result, nullValue()); }
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 void executePostValues(SeedRequest request,HttpPostRequestDecoder decoder){ List<InterfaceHttpData> params = decoder.getBodyHttpDatas(); for(InterfaceHttpData data :params){ MixedAttribute attribute = (MixedAttribute) data; try { request.setValue(new String(attribute.getName()), URLDecoder.decode(new String(attribute.content().array()),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
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 String finishUpload() throws Exception { String pathTo = null; try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data != null) { if (data instanceof DiskFileUpload) { DiskFileUpload diskFileUpload = (DiskFileUpload) data; Path tmpFile = diskFileUpload.getFile().toPath(); String uploadedFilename = diskFileUpload.getFilename(); String extension = ""; if (uploadedFilename.contains(".")) { extension = uploadedFilename.substring(uploadedFilename.lastIndexOf("."), uploadedFilename.length()); } String finalName = tmpFile.getFileName().toString() + extension; //this is just to make it work on team city. Path staticPath = Paths.get(staticFolderPath, uploadFolder); if (!Files.exists(staticPath)) { Files.createDirectories(staticPath); } Files.move(tmpFile, Paths.get(staticFolderPath, uploadFolder, finalName), StandardCopyOption.REPLACE_EXISTING); pathTo = uploadFolder + finalName; } data.release(); } } } catch (EndOfDataDecoderException endOfData) { //ignore. that's fine. } finally { // destroy the decoder to release all resources decoder.destroy(); decoder = null; } return pathTo; }
@Override public Set<String> keys() { return unmodifiableSet(decoder .getBodyHttpDatas() .stream() .map(InterfaceHttpData::getName) .collect(toSet())); }
/** * * @param factory * the factory used to create InterfaceHttpData * @param request * the request to encode * @param multipart * True if the FORM is a ENCTYPE="multipart/form-data" * @param charset * the charset to use as default * @param encoderMode * the mode for the encoder to use. See {@link EncoderMode} for the details. * @throws NullPointerException * for request or charset or factory * @throws ErrorDataEncoderException * if the request is not a POST */ public MyHttpPostRequestEncoder( HttpDataFactory factory, HttpRequest request, boolean multipart, Charset charset, EncoderMode encoderMode) throws ErrorDataEncoderException { if (factory == null) { throw new NullPointerException("factory"); } if (request == null) { throw new NullPointerException("request"); } if (charset == null) { throw new NullPointerException("charset"); } if (request.getMethod() != HttpMethod.POST) { throw new ErrorDataEncoderException("Cannot create a Encoder if not a POST"); } this.request = request; this.charset = charset; this.factory = factory; // Fill default values bodyListDatas = new ArrayList<InterfaceHttpData>(); // default mode isLastChunk = false; isLastChunkSent = false; isMultipart = multipart; multipartHttpDatas = new ArrayList<InterfaceHttpData>(); this.encoderMode = encoderMode; if (isMultipart) { initDataMultipart(); } }
/** * Set the Body HttpDatas list * * @throws NullPointerException * for datas * @throws ErrorDataEncoderException * if the encoding is in error or if the finalize were already done */ public void setBodyHttpDatas(List<InterfaceHttpData> datas) throws ErrorDataEncoderException { if (datas == null) { throw new NullPointerException("datas"); } globalBodySize = 0; bodyListDatas.clear(); currentFileUpload = null; duringMixedMode = false; multipartHttpDatas.clear(); for (InterfaceHttpData data : datas) { addBodyHttpData(data); } }
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; }
/** * {@inheritDoc} * <p/> * Prepares the request for reading by decoding all the content added via {@link #addContent(HttpContent)}. * @throws RestServiceException if request channel is closed or if the request could not be decoded/prepared. */ @Override public void prepare() throws RestServiceException { if (!isOpen()) { nettyMetrics.multipartRequestAlreadyClosedError.inc(); throw new RestServiceException("Request is closed", RestServiceErrorCode.RequestChannelClosed); } else if (!readyForRead) { // make sure data is held in memory. HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false); HttpPostMultipartRequestDecoder postRequestDecoder = new HttpPostMultipartRequestDecoder(httpDataFactory, request); try { HttpContent httpContent = rawRequestContents.poll(); while (httpContent != null) { try { // if the request is also an instance of HttpContent, the HttpPostMultipartRequestDecoder does the offer // automatically at the time of construction. We should not add it again. if (httpContent != request) { postRequestDecoder.offer(httpContent); } } finally { ReferenceCountUtil.release(httpContent); } httpContent = rawRequestContents.poll(); } for (InterfaceHttpData part : postRequestDecoder.getBodyHttpDatas()) { processPart(part); } requestContents.add(LastHttpContent.EMPTY_LAST_CONTENT); readyForRead = true; } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) { nettyMetrics.multipartRequestDecodeError.inc(); throw new RestServiceException("There was an error decoding the request", e, RestServiceErrorCode.MalformedRequest); } finally { postRequestDecoder.destroy(); } } }
@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 List<InterfaceHttpData> getMultipartParts() { return null; }
/** * Standard post without multipart but already support on Factory (memory management) * * @return the list of HttpData object (attribute and file) to be reused on next post */ private static List<InterfaceHttpData> formpost( Bootstrap bootstrap, String host, int port, URI uriSimple, File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception { // XXX /formpost // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriSimple.toASCIIString()); // Use the PostBody encoder HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, false); // false => not multipart // it is legal to add directly header or cookie into the request until finalize for (Entry<String, String> entry : headers) { request.headers().set(entry.getKey(), entry.getValue()); } // add Form attribute bodyRequestEncoder.addBodyAttribute("getform", "POST"); bodyRequestEncoder.addBodyAttribute("info", "first value"); bodyRequestEncoder.addBodyAttribute("secondinfo", "secondvalue ���&"); bodyRequestEncoder.addBodyAttribute("thirdinfo", textArea); bodyRequestEncoder.addBodyAttribute("fourthinfo", textAreaLong); bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false); // finalize request request = bodyRequestEncoder.finalizeRequest(); // Create the bodylist to be reused on the last version with Multipart support List<InterfaceHttpData> bodylist = bodyRequestEncoder.getBodyListAttributes(); // send request channel.write(request); // test if request was chunked and if so, finish the write if (bodyRequestEncoder.isChunked()) { // could do either request.isChunked() // either do it through ChunkedWriteHandler channel.write(bodyRequestEncoder); } channel.flush(); // Do not clear here since we will reuse the InterfaceHttpData on the next request // for the example (limit action on client side). Take this as a broadcast of the same // request on both Post actions. // // On standard program, it is clearly recommended to clean all files after each request // bodyRequestEncoder.cleanFiles(); // Wait for the server to close the connection. channel.closeFuture().sync(); return bodylist; }
/** * Multipart example */ private static void formpostmultipart( Bootstrap bootstrap, String host, int port, URI uriFile, HttpDataFactory factory, Iterable<Entry<String, String>> headers, List<InterfaceHttpData> bodylist) throws Exception { // XXX /formpostmultipart // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriFile.toASCIIString()); // Use the PostBody encoder HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, true); // true => multipart // it is legal to add directly header or cookie into the request until finalize for (Entry<String, String> entry : headers) { request.headers().set(entry.getKey(), entry.getValue()); } // add Form attribute from previous request in formpost() bodyRequestEncoder.setBodyHttpDatas(bodylist); // finalize request bodyRequestEncoder.finalizeRequest(); // send request channel.write(request); // test if request was chunked and if so, finish the write if (bodyRequestEncoder.isChunked()) { channel.write(bodyRequestEncoder); } channel.flush(); // Now no more use of file representation (and list of HttpData) bodyRequestEncoder.cleanFiles(); // Wait for the server to close the connection. channel.closeFuture().sync(); }