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);
}
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;
}
}
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);
}
}
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();
}
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);
}
}
Aggregations