List of usage examples for io.netty.handler.codec.http.multipart FileUpload getContentType
String getContentType();
From source file:firebats.http.server.exts.form.Form.java
License:Apache License
private static Form decodeWithContent(Context context, ByteBuf content) { //new DefaultHttpDataFactory(/*useDisk*/true)decoder? //Mix?16K???/*from w ww. java2 s.c o m*/ // final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(/*useDisk*/true),toNettyHttpRequest(context.request)); HttpServerRequest<ByteBuf> rxRequest = context.getRequest(); HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, rxRequest.getHttpMethod(), rxRequest.getUri()); for (Map.Entry<String, String> header : rxRequest.getHeaders().entries()) { nettyRequest.headers().add(header.getKey(), header.getValue()); } final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest); HttpContent httpContent = new DefaultHttpContent(content); decoder.offer(httpContent); decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT); Map<String, String> formParams = new LinkedHashMap<>(); Map<String, UploadedFile> files = new LinkedHashMap<>(); try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) { try { Attribute attr = (Attribute) data; if (!formParams.containsKey(data.getName())) { formParams.put(attr.getName(), attr.getValue()); } } catch (IOException e) { Throwables.propagate(e); } finally { //? data.release(); } } else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) { try { if (!files.containsKey(data.getName())) { final FileUpload nettyFileUpload = (FileUpload) data; final ByteBuf byteBuf = nettyFileUpload.content(); byteBuf.retain(); context.onComplete(new Action0() { @Override public void call() { if (log.isDebugEnabled()) { log.debug("form upload file release[" + data.getName() + ":" + nettyFileUpload.getFilename() + "]"); } byteBuf.release(); } }); UploadedFile fileUpload = new UploadedFile(nettyFileUpload.getFilename(), nettyFileUpload.getContentType(), byteBuf); files.put(data.getName(), fileUpload); } } finally { data.release(); } } } } catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) { // ignore } finally { decoder.destroy(); } Map<String, String> query = Form.toFlatQueryParams(context.getRequest().getQueryParameters()); return fromAll(query, formParams, files); }
From source file:holon.internal.http.netty.NettyServerHandler.java
License:Open Source License
private void storeFormData(InterfaceHttpData data) { try {/*w w w .j a v a 2s. co m*/ if (data.getHttpDataType() == HttpDataType.Attribute) { Attribute attribute = (Attribute) data; formParams.put(attribute.getName(), attribute.getValue()); } else { if (data.getHttpDataType() == HttpDataType.FileUpload) { FileUpload fileUpload = (FileUpload) data; if (fileUpload.isCompleted()) { File tempFile = File.createTempFile("holon", "upload"); fileUpload.renameTo(tempFile); formParams.put(fileUpload.getName(), new UploadedFile() { @Override public String fileName() { return fileUpload.getFilename(); } @Override public String contentType() { return fileUpload.getContentType(); } @Override public File file() { return tempFile; } }); } else { System.out.println("File upload should have been completed here!"); } } } } catch (IOException e1) { // Error while reading data from File, only print name and error e1.printStackTrace(); return; } }
From source file:io.syncframework.netty.RequestHandler.java
License:Apache License
/** * Example of reading request by chunk and getting values from chunk to chunk *///from w ww .ja v a2 s.co m private void readHttpDataChunkByChunk() throws Exception { try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data != null) { try { if (log.isTraceEnabled()) log.trace("data[{}]", data); // new value if (data.getHttpDataType() == HttpDataType.Attribute) { Attribute attribute = (Attribute) data; String value = attribute.getValue(); Map<String, List<String>> parameters = requestWrapper.getParameters(); List<String> values = parameters.get(attribute.getName()); if (values == null) values = new LinkedList<String>(); values.add(value); parameters.put(attribute.getName(), values); } else if (data.getHttpDataType() == HttpDataType.FileUpload) { FileUpload fileUpload = (FileUpload) data; if (fileUpload.getFilename() == null || fileUpload.getFilename().equals("")) { if (log.isTraceEnabled()) log.trace("fileupload filename is null or empty; returning"); decoder.removeHttpDataFromClean(fileUpload); return; } if (fileUpload.isCompleted()) { // in this case we shall place the file to the <application>/tmp directory String domain = this.request.headers().getAsString(HttpHeaderNames.HOST); if (log.isTraceEnabled()) log.trace("request host: {}", domain); int p = domain.indexOf(':'); if (p != -1) domain = domain.substring(0, p); Application application = ApplicationManager.getApplication(domain); if (application == null) { throw new RuntimeException( "no application found responsible for domain " + domain); } String applicationHome = (String) application.getContext() .get(ApplicationContext.HOME); String tmpDirectoryPath = applicationHome + File.separator + "tmp"; File tmpfile = new File(tmpDirectoryPath, fileUpload.getFilename()); if (fileUpload.isInMemory()) { if (!fileUpload.renameTo(tmpfile)) { throw new RuntimeException( "failed to place the upload file under the directory: " + tmpDirectoryPath); } } else { if (!fileUpload.getFile().renameTo(tmpfile)) { decoder.removeHttpDataFromClean(fileUpload); throw new RuntimeException( "failed to place the upload file under the directory: " + tmpDirectoryPath); } } io.syncframework.api.FileUpload fu = new io.syncframework.api.FileUpload(); fu.setName(fileUpload.getFilename()); fu.setType(fileUpload.getContentType()); fu.setFile(new File(tmpDirectoryPath, fileUpload.getFilename())); requestWrapper.getFiles().put(fileUpload.getName(), fu); } else { log.error("file yet to be completed but should not"); } } else { log.warn("why is falling into this category ?"); } } finally { data.release(); } } } } catch (EndOfDataDecoderException ignore) { // just ignore } }
From source file:io.werval.server.netty.NettyHttpFactories.java
License:Apache License
static Request requestOf(Charset defaultCharset, HttpBuildersSPI builders, String remoteSocketAddress, String identity, FullHttpRequest request) throws HttpRequestParsingException { ensureNotEmpty("Request Identity", identity); ensureNotNull("Netty FullHttpRequest", request); try {// w ww .j ava 2 s .com RequestBuilder builder = builders.newRequestBuilder().identifiedBy(identity) .remoteSocketAddress(remoteSocketAddress) .version(ProtocolVersion.valueOf(request.getProtocolVersion().text())) .method(request.getMethod().name()).uri(request.getUri()) .headers(headersToMap(request.headers())); if (request.content().readableBytes() > 0 && (POST.equals(request.getMethod()) || PUT.equals(request.getMethod()) || PATCH.equals(request.getMethod()))) { Optional<String> contentType = Headers.extractContentMimeType(request.headers().get(CONTENT_TYPE)); if (contentType.isPresent()) { switch (contentType.get()) { case APPLICATION_X_WWW_FORM_URLENCODED: case MULTIPART_FORM_DATA: try { Map<String, List<String>> attributes = new LinkedHashMap<>(); Map<String, List<Upload>> uploads = new LinkedHashMap<>(); HttpPostRequestDecoder postDecoder = new HttpPostRequestDecoder(request); for (InterfaceHttpData data : postDecoder.getBodyHttpDatas()) { switch (data.getHttpDataType()) { case Attribute: Attribute attribute = (Attribute) data; if (!attributes.containsKey(attribute.getName())) { attributes.put(attribute.getName(), new ArrayList<>()); } attributes.get(attribute.getName()).add(attribute.getValue()); break; case FileUpload: FileUpload fileUpload = (FileUpload) data; if (!uploads.containsKey(fileUpload.getName())) { uploads.put(fileUpload.getName(), new ArrayList<>()); } Upload upload; if (fileUpload.isInMemory()) { upload = new UploadInstance(fileUpload.getContentType(), fileUpload.getCharset(), fileUpload.getFilename(), new ByteBufByteSource(fileUpload.getByteBuf()).asBytes(), defaultCharset); } else { upload = new UploadInstance(fileUpload.getContentType(), fileUpload.getCharset(), fileUpload.getFilename(), fileUpload.getFile(), defaultCharset); } uploads.get(fileUpload.getName()).add(upload); break; default: break; } } builder = builder.bodyForm(attributes, uploads); break; } catch (ErrorDataDecoderException | IncompatibleDataDecoderException | NotEnoughDataDecoderException | IOException ex) { throw new WervalException("Form or multipart parsing error", ex); } default: builder = builder.bodyBytes(new ByteBufByteSource(request.content())); break; } } else { builder = builder.bodyBytes(new ByteBufByteSource(request.content())); } } return builder.build(); } catch (Exception ex) { throw new HttpRequestParsingException(ex); } }
From source file:ratpack.form.internal.FormDecoder.java
License:Apache License
public static Form parseForm(Context context, TypedData requestBody, MultiValueMap<String, String> base) throws RuntimeException { Request request = context.getRequest(); HttpMethod method = io.netty.handler.codec.http.HttpMethod.valueOf(request.getMethod().getName()); HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri()); nettyRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, request.getBody().getContentType().toString()); HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest); HttpContent content = new DefaultHttpContent(requestBody.getBuffer()); decoder.offer(content);//www . ja va 2 s .com decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT); Map<String, List<String>> attributes = new LinkedHashMap<>(base.getAll()); Map<String, List<UploadedFile>> files = new LinkedHashMap<>(); try { InterfaceHttpData data = decoder.next(); while (data != null) { if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) { List<String> values = attributes.get(data.getName()); if (values == null) { values = new ArrayList<>(1); attributes.put(data.getName(), values); } try { values.add(((Attribute) data).getValue()); } catch (IOException e) { throw uncheck(e); } finally { data.release(); } } else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) { List<UploadedFile> values = files.get(data.getName()); if (values == null) { values = new ArrayList<>(1); files.put(data.getName(), values); } try { FileUpload nettyFileUpload = (FileUpload) data; final ByteBuf byteBuf = nettyFileUpload.getByteBuf(); byteBuf.retain(); context.onClose(new Action<RequestOutcome>() { @Override public void execute(RequestOutcome thing) throws Exception { byteBuf.release(); } }); MediaType contentType; String rawContentType = nettyFileUpload.getContentType(); if (rawContentType == null) { contentType = null; } else { Charset charset = nettyFileUpload.getCharset(); if (charset == null) { contentType = DefaultMediaType.get(rawContentType); } else { contentType = DefaultMediaType.get(rawContentType + ";charset=" + charset); } } UploadedFile fileUpload = new DefaultUploadedFile( new ByteBufBackedTypedData(byteBuf, contentType), nettyFileUpload.getFilename()); values.add(fileUpload); } catch (IOException e) { throw uncheck(e); } finally { data.release(); } } data = decoder.next(); } } catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) { // ignore } finally { decoder.destroy(); } return new DefaultForm(new ImmutableDelegatingMultiValueMap<>(attributes), new ImmutableDelegatingMultiValueMap<>(files)); }
From source file:reactor.ipc.netty.http.client.HttpClientFormEncoder.java
License:Open Source License
/** * Add the InterfaceHttpData to the Body list * * @throws NullPointerException for data * @throws ErrorDataEncoderException if the encoding is in error or if the finalize * were already done */// w w w. java2 s .com void addBodyHttpData(InterfaceHttpData data) throws ErrorDataEncoderException { if (headerFinalized) { throw new ErrorDataEncoderException("Cannot add value once finalized"); } if (data == null) { throw new NullPointerException("data"); } bodyListDatas.add(data); if (!isMultipart) { if (data instanceof Attribute) { Attribute attribute = (Attribute) data; try { // name=value& with encoded name and attribute String key = encodeAttribute(attribute.getName(), charset); String value = encodeAttribute(attribute.getValue(), charset); Attribute newattribute = factory.createAttribute(request, key, value); multipartHttpDatas.add(newattribute); globalBodySize += newattribute.getName().length() + 1 + newattribute.length() + 1; } catch (IOException e) { throw new ErrorDataEncoderException(e); } } else if (data instanceof FileUpload) { // since not Multipart, only name=filename => Attribute FileUpload fileUpload = (FileUpload) data; // name=filename& with encoded name and filename String key = encodeAttribute(fileUpload.getName(), charset); String value = encodeAttribute(fileUpload.getFilename(), charset); Attribute newattribute = factory.createAttribute(request, key, value); multipartHttpDatas.add(newattribute); globalBodySize += newattribute.getName().length() + 1 + newattribute.length() + 1; } return; } /* * Logic: * if not Attribute: * add Data to body list * if (duringMixedMode) * add endmixedmultipart delimiter * currentFileUpload = null * duringMixedMode = false; * add multipart delimiter, multipart body header and Data to multipart list * reset currentFileUpload, duringMixedMode * if FileUpload: take care of multiple file for one field => mixed mode * if (duringMixeMode) * if (currentFileUpload.name == data.name) * add mixedmultipart delimiter, mixedmultipart body header and Data to multipart list * else * add endmixedmultipart delimiter, multipart body header and Data to multipart list * currentFileUpload = data * duringMixedMode = false; * else * if (currentFileUpload.name == data.name) * change multipart body header of previous file into multipart list to * mixedmultipart start, mixedmultipart body header * add mixedmultipart delimiter, mixedmultipart body header and Data to multipart list * duringMixedMode = true * else * add multipart delimiter, multipart body header and Data to multipart list * currentFileUpload = data * duringMixedMode = false; * Do not add last delimiter! Could be: * if duringmixedmode: endmixedmultipart + endmultipart * else only endmultipart */ if (data instanceof Attribute) { if (duringMixedMode) { InternalAttribute internal = new InternalAttribute(charset); internal.addValue("\r\n--" + multipartMixedBoundary + "--"); multipartHttpDatas.add(internal); multipartMixedBoundary = null; currentFileUpload = null; duringMixedMode = false; } InternalAttribute internal = new InternalAttribute(charset); if (!multipartHttpDatas.isEmpty()) { // previously a data field so CRLF internal.addValue("\r\n"); } internal.addValue("--" + multipartDataBoundary + "\r\n"); // content-disposition: form-data; name="field1" Attribute attribute = (Attribute) data; internal.addValue(HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + attribute.getName() + "\"\r\n"); // Add Content-Length: xxx internal.addValue(HttpHeaderNames.CONTENT_LENGTH + ": " + attribute.length() + "\r\n"); Charset localcharset = attribute.getCharset(); if (localcharset != null) { // Content-Type: text/plain; charset=charset internal.addValue(HttpHeaderNames.CONTENT_TYPE + ": " + DEFAULT_TEXT_CONTENT_TYPE + "; " + HttpHeaderValues.CHARSET + '=' + localcharset.name() + "\r\n"); } // CRLF between body header and data internal.addValue("\r\n"); multipartHttpDatas.add(internal); multipartHttpDatas.add(data); globalBodySize += attribute.length() + internal.size(); } else if (data instanceof FileUpload) { FileUpload fileUpload = (FileUpload) data; InternalAttribute internal = new InternalAttribute(charset); if (!multipartHttpDatas.isEmpty()) { // previously a data field so CRLF internal.addValue("\r\n"); } boolean localMixed; if (duringMixedMode) { if (currentFileUpload != null && currentFileUpload.getName().equals(fileUpload.getName())) { // continue a mixed mode localMixed = true; } else { // end a mixed mode // add endmixedmultipart delimiter, multipart body header // and // Data to multipart list internal.addValue("--" + multipartMixedBoundary + "--"); multipartHttpDatas.add(internal); multipartMixedBoundary = null; // start a new one (could be replaced if mixed start again // from here internal = new InternalAttribute(charset); internal.addValue("\r\n"); localMixed = false; // new currentFileUpload and no more in Mixed mode currentFileUpload = fileUpload; duringMixedMode = false; } } else { if (encoderMode != EncoderMode.HTML5 && currentFileUpload != null && currentFileUpload.getName().equals(fileUpload.getName())) { // create a new mixed mode (from previous file) // change multipart body header of previous file into // multipart list to // mixedmultipart start, mixedmultipart body header // change Internal (size()-2 position in multipartHttpDatas) // from (line starting with *) // --AaB03x // * Content-Disposition: form-data; name="files"; // filename="file1.txt" // Content-Type: text/plain // to (lines starting with *) // --AaB03x // * Content-Disposition: form-data; name="files" // * Content-Type: multipart/mixed; boundary=BbC04y // * // * --BbC04y // * Content-Disposition: attachment; filename="file1.txt" // Content-Type: text/plain initMixedMultipart(); InternalAttribute pastAttribute = (InternalAttribute) multipartHttpDatas .get(multipartHttpDatas.size() - 2); // remove past size globalBodySize -= pastAttribute.size(); StringBuilder replacement = new StringBuilder( 139 + multipartDataBoundary.length() + multipartMixedBoundary.length() * 2 + fileUpload.getFilename().length() + fileUpload.getName().length()) .append("--").append(multipartDataBoundary).append("\r\n") .append(HttpHeaderNames.CONTENT_DISPOSITION).append(": ") .append(HttpHeaderValues.FORM_DATA).append("; ") .append(HttpHeaderValues.NAME).append("=\"") .append(fileUpload.getName()).append("\"\r\n") .append(HttpHeaderNames.CONTENT_TYPE).append(": ") .append(HttpHeaderValues.MULTIPART_MIXED).append("; ") .append(HttpHeaderValues.BOUNDARY).append('=') .append(multipartMixedBoundary).append("\r\n\r\n") .append("--").append(multipartMixedBoundary).append("\r\n") .append(HttpHeaderNames.CONTENT_DISPOSITION).append(": ") .append(HttpHeaderValues.ATTACHMENT).append("; "); if (!fileUpload.getFilename().isEmpty()) { replacement.append(HttpHeaderValues.FILENAME).append("=\"") .append(fileUpload.getFilename()); } replacement.append("\"\r\n"); pastAttribute.setValue(replacement.toString(), 1); pastAttribute.setValue("", 2); // update past size globalBodySize += pastAttribute.size(); // now continue // add mixedmultipart delimiter, mixedmultipart body header // and // Data to multipart list localMixed = true; duringMixedMode = true; } else { // a simple new multipart // add multipart delimiter, multipart body header and Data // to multipart list localMixed = false; currentFileUpload = fileUpload; duringMixedMode = false; } } if (localMixed) { // add mixedmultipart delimiter, mixedmultipart body header and // Data to multipart list internal.addValue("--" + multipartMixedBoundary + "\r\n"); // Content-Disposition: attachment; filename="file1.txt" if (!fileUpload.getFilename().isEmpty()) { internal.addValue(HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.ATTACHMENT + "; " + HttpHeaderValues.FILENAME + "=\"" + fileUpload.getFilename() + "\"\r\n"); } else { internal.addValue( HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.ATTACHMENT + ";\r\n"); } } else { internal.addValue("--" + multipartDataBoundary + "\r\n"); // Content-Disposition: form-data; name="files"; // filename="file1.txt" if (!fileUpload.getFilename().isEmpty()) { internal.addValue(HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + fileUpload.getName() + "\"; " + HttpHeaderValues.FILENAME + "=\"" + fileUpload.getFilename() + "\"\r\n"); } else { internal.addValue(HttpHeaderNames.CONTENT_DISPOSITION + ": " + HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + fileUpload.getName() + "\";\r\n"); } } // Add Content-Length: xxx internal.addValue(HttpHeaderNames.CONTENT_LENGTH + ": " + fileUpload.length() + "\r\n"); // Content-Type: image/gif // Content-Type: text/plain; charset=ISO-8859-1 // Content-Transfer-Encoding: binary internal.addValue(HttpHeaderNames.CONTENT_TYPE + ": " + fileUpload.getContentType()); String contentTransferEncoding = fileUpload.getContentTransferEncoding(); if (contentTransferEncoding != null && contentTransferEncoding.equals(DEFAULT_TRANSFER_ENCODING)) { internal.addValue("\r\n" + HttpHeaderNames.CONTENT_TRANSFER_ENCODING + ": " + DEFAULT_BINARY_CONTENT_TYPE + "\r\n\r\n"); } else if (fileUpload.getCharset() != null) { internal.addValue( "; " + HttpHeaderValues.CHARSET + '=' + fileUpload.getCharset().name() + "\r\n\r\n"); } else { internal.addValue("\r\n\r\n"); } multipartHttpDatas.add(internal); multipartHttpDatas.add(data); globalBodySize += fileUpload.length() + internal.size(); } }