Example usage for io.netty.handler.codec.http.multipart Attribute getValue

List of usage examples for io.netty.handler.codec.http.multipart Attribute getValue

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.multipart Attribute getValue.

Prototype

String getValue() throws IOException;

Source Link

Document

Returns the value of this HttpData.

Usage

From source file:HttpUploadServerHandler.java

License:Apache License

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;/*  w  w w. j  av a  2  s.  c o  m*/
        try {
            value = attribute.getValue();
        } catch (IOException e1) {
            // Error while reading data from File, only print name and error
            e1.printStackTrace();
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.getName() + " Error while reading value: " + e1.getMessage() + "\r\n");
            return;
        }
        if (value.length() > 100) {
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.getName() + " data too long\r\n");
        } else {
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.toString() + "\r\n");
        }
    } else {
        responseContent.append(
                "\r\nBODY FileUpload: " + data.getHttpDataType().name() + ": " + data.toString() + "\r\n");
        if (data.getHttpDataType() == HttpDataType.FileUpload) {
            FileUpload fileUpload = (FileUpload) data;
            if (fileUpload.isCompleted()) {
                if (fileUpload.length() < 10000) {
                    responseContent.append("\tContent of file\r\n");
                    try {
                        responseContent.append(fileUpload.getString(fileUpload.getCharset()));
                    } catch (IOException e1) {
                        // do nothing for the example
                        e1.printStackTrace();
                    }
                    responseContent.append("\r\n");
                } else {
                    responseContent.append("\tFile too long to be printed out:" + fileUpload.length() + "\r\n");
                }
                // fileUpload.isInMemory();// tells if the file is in Memory
                // or on File
                // fileUpload.renameTo(dest); // enable to move into another
                // File dest
                // decoder.removeFileUploadFromClean(fileUpload); //remove
                // the File of to delete file
            } else {
                responseContent.append("\tFile to be continued but should not!\r\n");
            }
        }
    }
    //BufUtil.release(data);
}

From source file:cc.io.lessons.server.HttpUploadServerHandler.java

License:Apache License

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;/*from w  w w.  j a  va  2 s.co m*/
        try {
            value = attribute.getValue();
        } catch (IOException e1) {
            // Error while reading data from File, only print name and error
            e1.printStackTrace();
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.getName() + " Error while reading value: " + e1.getMessage() + "\r\n");
            return;
        }
        if (value.length() > 100) {
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.getName() + " data too long\r\n");
        } else {
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.toString() + "\r\n");
        }
    } else {
        responseContent.append(
                "\r\nBODY FileUpload: " + data.getHttpDataType().name() + ": " + data.toString() + "\r\n");
        if (data.getHttpDataType() == HttpDataType.FileUpload) {
            FileUpload fileUpload = (FileUpload) data;
            if (fileUpload.isCompleted()) {
                if (fileUpload.length() < 10000) {
                    responseContent.append("\tContent of file\r\n");
                    try {
                        responseContent.append(fileUpload.getString(fileUpload.getCharset()));
                    } catch (IOException e1) {
                        // do nothing for the example
                        e1.printStackTrace();
                    }
                    responseContent.append("\r\n");
                } else {
                    responseContent.append("\tFile too long to be printed out:" + fileUpload.length() + "\r\n");
                }
                // fileUpload.isInMemory();// tells if the file is in Memory
                // or on File
                // fileUpload.renameTo(dest); // enable to move into another
                // File dest
                // decoder.removeFileUploadFromClean(fileUpload); //remove
                // the File of to delete file
            } else {
                responseContent.append("\tFile to be continued but should not!\r\n");
            }
        }
    }
}

From source file:cn.wantedonline.puppy.httpserver.component.HttpRequest.java

License:Apache License

private Map<String, List<String>> initParametersByPost(Map<String, List<String>> params,
        HttpPostRequestDecoder httpPostRequestDecoder) {
    if (AssertUtil.isNull(httpPostRequestDecoder)) {
        return params;
    }//from   w  ww  .  ja  v a 2 s.co  m

    try {
        List<InterfaceHttpData> datas = httpPostRequestDecoder.getBodyHttpDatas();
        if (AssertUtil.isNotEmptyCollection(datas)) {
            for (InterfaceHttpData data : datas) {
                if (data instanceof Attribute) {
                    Attribute attribute = (Attribute) data;
                    try {
                        String key = attribute.getName();
                        String value = attribute.getValue();

                        List<String> ori = params.get(key);
                        if (AssertUtil.isEmptyCollection(ori)) {
                            ori = new ArrayList<>(1);
                            params.put(key, ori);
                        }
                        ori.add(value);
                    } catch (IOException e) {
                        log.error("cant init attribute,req:{},attribute:{}",
                                new Object[] { this, attribute, e });
                    }
                }
            }
        }
    } catch (HttpPostRequestDecoder.NotEnoughDataDecoderException e) {
        log.error("req:{}", this, e);
    }
    return params;
}

From source file:com.alibaba.dubbo.qos.command.decoder.HttpCommandDecoder.java

License:Apache License

public static CommandContext decode(HttpRequest request) {
    CommandContext commandContext = null;
    if (request != null) {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        String path = queryStringDecoder.path();
        String[] array = path.split("/");
        if (array.length == 2) {
            String name = array[1];

            // process GET request and POST request separately. Check url for GET, and check body for POST
            if (request.getMethod() == HttpMethod.GET) {
                if (queryStringDecoder.parameters().isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    List<String> valueList = new ArrayList<String>();
                    for (List<String> values : queryStringDecoder.parameters().values()) {
                        valueList.addAll(values);
                    }//from w  ww  .j  ava  2s. com
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}),
                            true);
                }
            } else if (request.getMethod() == HttpMethod.POST) {
                HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
                List<String> valueList = new ArrayList<String>();
                for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) {
                    if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                        Attribute attribute = (Attribute) interfaceHttpData;
                        try {
                            valueList.add(attribute.getValue());
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                if (valueList.isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}),
                            true);
                }
            }
        }
    }

    return commandContext;
}

From source file:com.android.tools.idea.diagnostics.crash.GoogleCrashTest.java

License:Apache License

@Ignore
@Test//from   w ww.  ja v a  2  s.  c  o m
public void checkServerReceivesPostedData() throws Exception {
    String expectedReportId = "deadcafe";
    Map<String, String> attributes = new ConcurrentHashMap<>();

    myTestServer.setResponseSupplier(httpRequest -> {
        if (httpRequest.method() != HttpMethod.POST) {
            return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
        }

        HttpPostRequestDecoder requestDecoder = new HttpPostRequestDecoder(httpRequest);
        try {
            for (InterfaceHttpData httpData : requestDecoder.getBodyHttpDatas()) {
                if (httpData instanceof Attribute) {
                    Attribute attr = (Attribute) httpData;
                    attributes.put(attr.getName(), attr.getValue());
                }
            }
        } catch (IOException e) {
            return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
        } finally {
            requestDecoder.destroy();
        }

        return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
                Unpooled.wrappedBuffer(expectedReportId.getBytes(UTF_8)));
    });

    CrashReport report = CrashReport.Builder.createForException(ourIndexNotReadyException)
            .setProduct("AndroidStudioTestProduct").setVersion("1.2.3.4").build();
    CompletableFuture<String> reportId = myReporter.submit(report);

    assertEquals(expectedReportId, reportId.get());

    // assert that the server get the expected data
    assertEquals("AndroidStudioTestProduct", attributes.get(GoogleCrash.KEY_PRODUCT_ID));
    assertEquals("1.2.3.4", attributes.get(GoogleCrash.KEY_VERSION));

    // Note: the exception message should have been elided
    assertEquals("com.intellij.openapi.project.IndexNotReadyException: <elided>\n" + STACK_TRACE,
            attributes.get(GoogleCrash.KEY_EXCEPTION_INFO));

    List<String> descriptions = Arrays.asList("2.3.0.0\n1.8.0_73-b02", "2.3.0.1\n1.8.0_73-b02");
    report = CrashReport.Builder.createForCrashes(descriptions).setProduct("AndroidStudioTestProduct")
            .setVersion("1.2.3.4").build();

    attributes.clear();

    reportId = myReporter.submit(report);
    assertEquals(expectedReportId, reportId.get());

    // check that the crash count and descriptions made through
    assertEquals(descriptions.size(), Integer.parseInt(attributes.get("numCrashes")));
    assertEquals("2.3.0.0\n1.8.0_73-b02\n\n2.3.0.1\n1.8.0_73-b02", attributes.get("crashDesc"));

    Path testData = Paths.get(AndroidTestBase.getTestDataPath());
    List<String> threadDump = Files.readAllLines(testData.resolve(Paths.get("threadDumps", "1.txt")), UTF_8);
    report = CrashReport.Builder.createForPerfReport("td.txt", Joiner.on('\n').join(threadDump)).build();

    attributes.clear();

    reportId = myReporter.submit(report);
    assertEquals(expectedReportId, reportId.get());
    assertEquals(threadDump.stream().collect(Collectors.joining("\n")), attributes.get("td.txt"));
}

From source file:com.bay1ts.bay.core.Request.java

License:Apache License

/**
 * ?Content-Type:application/x-www-form-urlencoded post body ?(htmlform)
 *
 * @param name ??form post inputname//from w w  w  . j  a  v  a  2s  .c o m
 * @return value
 */
public String postBody(String name) {
    HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(
            new DefaultHttpDataFactory(false), this.fullHttpRequest);
    InterfaceHttpData data = httpPostRequestDecoder.getBodyHttpData(name);
    if (data != null && data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value = null;
        try {
            value = attribute.getValue();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            data.release();
        }
        return value;
    }
    return null;
}

From source file:com.bay1ts.bay.core.Request.java

License:Apache License

public Set<String> postBodyValues() {
    HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(
            new DefaultHttpDataFactory(false), this.fullHttpRequest);
    Set<String> set = new HashSet<>();
    for (; httpPostRequestDecoder.hasNext();) {
        InterfaceHttpData data = httpPostRequestDecoder.next();
        if (data != null) {
            try {
                Attribute attribute = (Attribute) data;
                set.add(attribute.getValue());
            } catch (IOException e) {
                e.printStackTrace();//from w  w  w. jav a  2  s  .  c  o m
            } finally {
                data.release();
            }
        }
    }
    return set;
}

From source file:com.cats.version.httpserver.VersionProtocolMessageHandler.java

License:Apache License

private String decodeMessage(FullHttpRequest request) throws IOException {
    HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
    InterfaceHttpData data = httpPostRequestDecoder.getBodyHttpData("msg");
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String strMsg = attribute.getValue();
        return strMsg;
    }//from  w w  w. j av a2s . co  m
    return null;
}

From source file:com.chiorichan.http.HttpHandler.java

License:Mozilla Public License

private void writeHttpData(InterfaceHttpData data) throws IOException {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;/*from   w w  w  .  j  a va  2  s .  c  om*/
        try {
            value = attribute.getValue();
        } catch (IOException e) {
            e.printStackTrace();
            response.sendException(e);
            return;
        }

        request.putPostMap(attribute.getName(), value);

        /*
         * Should resolve the problem described in Issue #9 on our GitHub
         */
        attribute.delete();
    } else if (data.getHttpDataType() == HttpDataType.FileUpload) {
        FileUpload fileUpload = (FileUpload) data;
        if (fileUpload.isCompleted())
            try {
                request.putUpload(fileUpload.getName(), new UploadedFile(fileUpload));
            } catch (IOException e) {
                e.printStackTrace();
                response.sendException(e);
            }
        else
            NetworkManager.getLogger().warning("File to be continued but should not!");
    }
}

From source file:com.cmz.http.upload.HttpUploadServerHandler.java

License:Apache License

private void writeHttpData(InterfaceHttpData data) {
    if (data.getHttpDataType() == HttpDataType.Attribute) {
        Attribute attribute = (Attribute) data;
        String value;//from   w  w w  .j av a  2 s . c  o  m
        try {
            value = attribute.getValue();
        } catch (IOException e1) {
            // Error while reading data from File, only print name and error
            e1.printStackTrace();
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.getName() + " Error while reading value: " + e1.getMessage() + "\r\n");
            return;
        }
        if (value.length() > 100) {
            responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": "
                    + attribute.getName() + " data too long\r\n");
        } else {
            responseContent.append(
                    "\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": " + attribute + "\r\n");
        }
    } else {
        responseContent.append("\r\nBODY FileUpload: " + data.getHttpDataType().name() + ": " + data + "\r\n");
        if (data.getHttpDataType() == HttpDataType.FileUpload) {
            FileUpload fileUpload = (FileUpload) data;
            if (fileUpload.isCompleted()) {
                if (fileUpload.length() < 10000) {
                    responseContent.append("\tContent of file\r\n");
                    try {
                        responseContent.append(fileUpload.getString(fileUpload.getCharset()));
                    } catch (IOException e1) {
                        // do nothing for the example
                        e1.printStackTrace();
                    }
                    responseContent.append("\r\n");
                } else {
                    responseContent.append("\tFile too long to be printed out:" + fileUpload.length() + "\r\n");
                }
                try {

                    fileUpload.setCharset(Charset.forName("UTF-8"));
                    fileUpload.renameTo(new File("11.txt"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // fileUpload.isInMemory();// tells if the file is in Memory
                // or on File
                // fileUpload.renameTo(dest); // enable to move into another
                // File dest
                // decoder.removeFileUploadFromClean(fileUpload); //remove
                // the File of to delete file
            } else {
                responseContent.append("\tFile to be continued but should not!\r\n");
            }
        }
    }
}