Search in sources :

Example 6 with Charset

use of java.nio.charset.Charset in project jetty.project by eclipse.

the class MultiPartContentProviderTest method testFieldWithFile.

@Test
public void testFieldWithFile() throws Exception {
    // Prepare a file to upload.
    byte[] data = new byte[1024];
    new Random().nextBytes(data);
    Path tmpDir = MavenTestingUtils.getTargetTestingPath();
    Path tmpPath = Files.createTempFile(tmpDir, "multipart_", ".txt");
    try (OutputStream output = Files.newOutputStream(tmpPath, StandardOpenOption.CREATE)) {
        output.write(data);
    }
    String field = "field";
    String value = "€";
    String fileField = "file";
    Charset encoding = StandardCharsets.UTF_8;
    String contentType = "text/plain;charset=" + encoding.name();
    String headerName = "foo";
    String headerValue = "bar";
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Part> parts = new ArrayList<>(request.getParts());
            Assert.assertEquals(2, parts.size());
            Part fieldPart = parts.get(0);
            Part filePart = parts.get(1);
            if (!field.equals(fieldPart.getName())) {
                Part swap = filePart;
                filePart = fieldPart;
                fieldPart = swap;
            }
            Assert.assertEquals(field, fieldPart.getName());
            Assert.assertEquals(contentType, fieldPart.getContentType());
            Assert.assertEquals(value, IO.toString(fieldPart.getInputStream(), encoding));
            Assert.assertEquals(headerValue, fieldPart.getHeader(headerName));
            Assert.assertEquals(fileField, filePart.getName());
            Assert.assertEquals("application/octet-stream", filePart.getContentType());
            Assert.assertEquals(tmpPath.getFileName().toString(), filePart.getSubmittedFileName());
            Assert.assertEquals(Files.size(tmpPath), filePart.getSize());
            Assert.assertArrayEquals(data, IO.readBytes(filePart.getInputStream()));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    HttpFields fields = new HttpFields();
    fields.put(headerName, headerValue);
    multiPart.addFieldPart(field, new StringContentProvider(value, encoding), fields);
    multiPart.addFilePart(fileField, tmpPath.getFileName().toString(), new PathContentProvider(tmpPath), null);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertEquals(200, response.getStatus());
    Files.delete(tmpPath);
}
Also used : Path(java.nio.file.Path) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) Part(javax.servlet.http.Part) HttpFields(org.eclipse.jetty.http.HttpFields) ArrayList(java.util.ArrayList) List(java.util.List) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 7 with Charset

use of java.nio.charset.Charset in project vert.x by eclipse.

the class VertxHttp2NetSocket method write.

@Override
public NetSocket write(String str, String enc) {
    synchronized (conn) {
        Charset cs = enc != null ? Charset.forName(enc) : CharsetUtil.UTF_8;
        writeData(Unpooled.copiedBuffer(str, cs), false);
        return this;
    }
}
Also used : Charset(java.nio.charset.Charset)

Example 8 with Charset

use of java.nio.charset.Charset in project tomcat by apache.

the class OutputBuffer method setConverter.

private void setConverter() throws IOException {
    if (coyoteResponse != null) {
        enc = coyoteResponse.getCharacterEncoding();
    }
    if (enc == null) {
        enc = org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING;
    }
    final Charset charset = getCharset(enc);
    conv = encoders.get(charset);
    if (conv == null) {
        conv = createConverter(charset);
        encoders.put(charset, conv);
    }
}
Also used : Charset(java.nio.charset.Charset)

Example 9 with Charset

use of java.nio.charset.Charset in project tomcat by apache.

the class ApplicationPushBuilder method decode.

// Package private so it can be tested. charsetName must be in lower case.
static String decode(String input, String charsetName) {
    int start = input.indexOf('%');
    int end = 0;
    // Shortcut
    if (start == -1) {
        return input;
    }
    Charset charset;
    try {
        charset = B2CConverter.getCharsetLower(charsetName);
    } catch (UnsupportedEncodingException uee) {
        // before reaching here
        throw new IllegalStateException(uee);
    }
    StringBuilder result = new StringBuilder(input.length());
    while (start != -1) {
        // Found the start of a %nn sequence. Copy everything form the last
        // end to this start to the output.
        result.append(input.substring(end, start));
        // Advance the end 3 characters: %nn
        end = start + 3;
        while (end < input.length() && input.charAt(end) == '%') {
            end += 3;
        }
        result.append(decode(input.substring(start, end), charset));
        start = input.indexOf('%', end);
    }
    // Append the remaining text
    result.append(input.substring(end));
    return result.toString();
}
Also used : Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with Charset

use of java.nio.charset.Charset in project jersey by jersey.

the class FreemarkerViewProcessor method writeTo.

@Override
public void writeTo(final Template template, final Viewable viewable, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream out) throws IOException {
    try {
        Object model = viewable.getModel();
        if (!(model instanceof Map)) {
            model = new HashMap<String, Object>() {

                {
                    put("model", viewable.getModel());
                }
            };
        }
        Charset encoding = setContentType(mediaType, httpHeaders);
        template.process(model, new OutputStreamWriter(out, encoding));
    } catch (TemplateException te) {
        throw new ContainerException(te);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) ContainerException(org.glassfish.jersey.server.ContainerException) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Aggregations

Charset (java.nio.charset.Charset)1400 IOException (java.io.IOException)259 Test (org.junit.Test)186 InputStream (java.io.InputStream)114 ByteBuffer (java.nio.ByteBuffer)110 File (java.io.File)104 ArrayList (java.util.ArrayList)102 InputStreamReader (java.io.InputStreamReader)99 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)64 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 CharsetDecoder (java.nio.charset.CharsetDecoder)55 ByteArrayInputStream (java.io.ByteArrayInputStream)54 List (java.util.List)54 Path (java.nio.file.Path)50 CharsetEncoder (java.nio.charset.CharsetEncoder)49 FileInputStream (java.io.FileInputStream)48 OutputStream (java.io.OutputStream)47