/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@code UTF8} charset is utilized. * * @param media the media type. * @return the character set. */ public static Charset getCharset(final MediaType media) { return ReaderWriter.getCharset(media); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@link #UTF8} charset is utilized. * * @param m the media type. * @return the character set. */ public static Charset getCharset(MediaType m) { return ReaderWriter.getCharset(m); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@code UTF8} charset is utilized. * * @param media the media type. * @return the character set. */ public static Charset getCharset(final MediaType media) { return ReaderWriter.getCharset(media); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@link #UTF8} charset is utilized. * * @param m the media type. * @return the character set. */ public static Charset getCharset(MediaType m) { return ReaderWriter.getCharset(m); }
/** * Convert a string to bytes and write those bytes to an output stream. * * @param s the string to convert to bytes. * @param out the output stream to write to. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @throws IOException in case of a write failure. */ public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException { Writer osw = new OutputStreamWriter(out, getCharset(type)); osw.write(s, 0, s.length()); osw.flush(); }
/** * Convert a string to bytes and write those bytes to an output stream. * * @param s the string to convert to bytes. * @param out the output stream to write to. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @throws IOException in case of a write failure. */ public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException { Writer osw = new OutputStreamWriter(out, getCharset(type)); osw.write(s, 0, s.length()); osw.flush(); }
/** * Read the bytes of an input stream and convert to a string. * * @param in the input stream to read from. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @return the string. * * @throws IOException if there is an error reading from the input stream. */ public static String readFromAsString(InputStream in, MediaType type) throws IOException { return readFromAsString(new InputStreamReader(in, getCharset(type))); }
/** * Read the bytes of an input stream and convert to a string. * * @param in the input stream to read from. * @param type the media type that determines the character set defining * how to decode bytes to characters. * @return the string. * * @throws IOException if there is an error reading from the input stream. */ public static String readFromAsString(InputStream in, MediaType type) throws IOException { return readFromAsString(new InputStreamReader(in, getCharset(type))); }
public <M extends MultivaluedMap<String, String>> void writeTo( M t, MediaType mediaType, OutputStream entityStream) throws IOException { final String charsetName = ReaderWriter.getCharset(mediaType).name(); final StringBuilder sb = new StringBuilder(); for (Map.Entry<String, List<String>> e : t.entrySet()) { for (String value : e.getValue()) { if (sb.length() > 0) { sb.append('&'); } sb.append(URLEncoder.encode(e.getKey(), charsetName)); if (value != null) { sb.append('='); sb.append(URLEncoder.encode(value, charsetName)); } } } writeToAsString(sb.toString(), entityStream, mediaType); } }
public <M extends MultivaluedMap<String, String>> void writeTo( M t, MediaType mediaType, OutputStream entityStream) throws IOException { final String charsetName = ReaderWriter.getCharset(mediaType).name(); final StringBuilder sb = new StringBuilder(); for (Map.Entry<String, List<String>> e : t.entrySet()) { for (String value : e.getValue()) { if (sb.length() > 0) { sb.append('&'); } sb.append(URLEncoder.encode(e.getKey(), charsetName)); if (value != null) { sb.append('='); sb.append(URLEncoder.encode(value, charsetName)); } } } writeToAsString(sb.toString(), entityStream, mediaType); } }
public <M extends MultivaluedMap<String, String>> M readFrom(M map, MediaType mediaType, boolean decode, InputStream entityStream) throws IOException { final String encoded = readFromAsString(entityStream, mediaType); final String charsetName = ReaderWriter.getCharset(mediaType).name(); final StringTokenizer tokenizer = new StringTokenizer(encoded, "&"); String token; try { while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); int idx = token.indexOf('='); if (idx < 0) { map.add(decode ? URLDecoder.decode(token, charsetName) : token, null); } else if (idx > 0) { if (decode) { map.add(URLDecoder.decode(token.substring(0, idx), charsetName), URLDecoder.decode(token.substring(idx + 1), charsetName)); } else { map.add(token.substring(0, idx), token.substring(idx + 1)); } } } return map; } catch (IllegalArgumentException ex) { throw new BadRequestException(ex); } }
public <M extends MultivaluedMap<String, String>> M readFrom(M map, MediaType mediaType, boolean decode, InputStream entityStream) throws IOException { final String encoded = readFromAsString(entityStream, mediaType); final String charsetName = ReaderWriter.getCharset(mediaType).name(); final StringTokenizer tokenizer = new StringTokenizer(encoded, "&"); String token; try { while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); int idx = token.indexOf('='); if (idx < 0) { map.add(decode ? URLDecoder.decode(token, charsetName) : token, null); } else if (idx > 0) { if (decode) { map.add(URLDecoder.decode(token.substring(0, idx), charsetName), URLDecoder.decode(token.substring(idx + 1), charsetName)); } else { map.add(token.substring(0, idx), token.substring(idx + 1)); } } } return map; } catch (IllegalArgumentException ex) { throw new BadRequestException(ex); } }
private Form switchUrlEncoding(final ContainerRequest request, final Form otherForm) { final Set<Map.Entry<String, List<String>>> entries = otherForm.asMap().entrySet(); MultivaluedMap<String, String> formMap = new NullableMultivaluedHashMap<>(); for (Map.Entry<String, List<String>> entry : entries) { final String charsetName = ReaderWriter.getCharset(MediaType.valueOf( request.getHeaderString(HttpHeaders.CONTENT_TYPE))).name(); String key; try { key = decode ? URLDecoder.decode(entry.getKey(), charsetName) : URLEncoder.encode(entry.getKey(), charsetName); for (String value : entry.getValue()) { if (value != null) { formMap.add(key, decode ? URLDecoder.decode(value, charsetName) : URLEncoder.encode(value, charsetName)); } else { formMap.add(key, null); } } } catch (UnsupportedEncodingException uee) { throw new ProcessingException(LocalizationMessages.ERROR_UNSUPPORTED_ENCODING(charsetName, extractor.getName()), uee); } } return new Form(formMap); }
private Form switchUrlEncoding(final ContainerRequest request, final Form otherForm) { final Set<Map.Entry<String, List<String>>> entries = otherForm.asMap().entrySet(); MultivaluedMap<String, String> formMap = new NullableMultivaluedHashMap<>(); for (Map.Entry<String, List<String>> entry : entries) { final String charsetName = ReaderWriter.getCharset(MediaType.valueOf( request.getHeaderString(HttpHeaders.CONTENT_TYPE))).name(); String key; try { key = decode ? URLDecoder.decode(entry.getKey(), charsetName) : URLEncoder.encode(entry.getKey(), charsetName); for (String value : entry.getValue()) { if (value != null) { formMap.add(key, decode ? URLDecoder.decode(value, charsetName) : URLEncoder.encode(value, charsetName)); } else { formMap.add(key, null); } } } catch (UnsupportedEncodingException uee) { throw new ProcessingException(LocalizationMessages.ERROR_UNSUPPORTED_ENCODING(charsetName, extractor.getName()), uee); } } return new Form(formMap); }
private Form switchUrlEncoding(final ContainerRequest request, final Form otherForm) { final Set<Map.Entry<String, List<String>>> entries = otherForm.asMap().entrySet(); MultivaluedMap<String, String> formMap = new NullableMultivaluedHashMap<>(); for (Map.Entry<String, List<String>> entry : entries) { final String charsetName = ReaderWriter.getCharset(MediaType.valueOf( request.getHeaderString(HttpHeaders.CONTENT_TYPE))).name(); String key; try { key = decode ? URLDecoder.decode(entry.getKey(), charsetName) : URLEncoder.encode(entry.getKey(), charsetName); for (String value : entry.getValue()) { if (value != null) { formMap.add(key, decode ? URLDecoder.decode(value, charsetName) : URLEncoder.encode(value, charsetName)); } else { formMap.add(key, null); } } } catch (UnsupportedEncodingException uee) { throw new ProcessingException(LocalizationMessages.ERROR_UNSUPPORTED_ENCODING(charsetName, extractor.getName()), uee); } } return new Form(formMap); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@code UTF8} charset is utilized. * * @param media the media type. * @return the character set. */ public static Charset getCharset(final MediaType media) { return ReaderWriter.getCharset(media); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@code UTF8} charset is utilized. * * @param media the media type. * @return the character set. */ public static Charset getCharset(final MediaType media) { return ReaderWriter.getCharset(media); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@link #UTF8} charset is utilized. * * @param m the media type. * @return the character set. */ public static Charset getCharset(MediaType m) { return ReaderWriter.getCharset(m); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@code UTF8} charset is utilized. * * @param media the media type. * @return the character set. */ public static Charset getCharset(final MediaType media) { return ReaderWriter.getCharset(media); }
/** * Get the character set from a media type. * <p> * The character set is obtained from the media type parameter "charset". * If the parameter is not present the {@link #UTF8} charset is utilized. * * @param m the media type. * @return the character set. */ public static Charset getCharset(MediaType m) { return ReaderWriter.getCharset(m); }