/** * Decodes a filename from the Content-Disposition header value according to * RFC 2183 and RFC 2231. * <p> * See <a href="http://tools.ietf.org/html/rfc2231">RFC 2231</a> for * details. * * @param value * the header value to decode * @return the filename */ public static String decodeContentDispositionFilename(String value) { Map<String, String> params = new HashMap<String, String>(); decodeContentDisposition(value, params); return params.get(DISPOSITION_FILENAME); }
/** * Encodes the Content-Disposition header value according to RFC 2183 and * RFC 2231. * <p> * See <a href="http://tools.ietf.org/html/rfc2231">RFC 2231</a> for * details. * * @param disposition * the disposition * @param filename * the file name * @return the encoded header value */ public static String encodeContentDisposition(String disposition, String filename) { if (disposition == null) { disposition = DISPOSITION_ATTACHMENT; } return disposition + encodeRFC2231(DISPOSITION_FILENAME, filename); }
/** * Encodes a MIME parameter per RFC 2231. * <p> * This implementation always uses UTF-8 and no language. * <p> * See <a href="http://tools.ietf.org/html/rfc2231">RFC 2231</a> for * details. * * @param value * the string to encode * @return the encoded string */ protected static String encodeRFC2231(String key, String value) { StringBuilder buf = new StringBuilder(32); boolean encoded = encodeRFC2231value(value, buf); if (encoded) { return "; " + key + "*=" + buf.toString(); } else { return "; " + key + "=" + value; } }
protected static String decodeRFC2231value(String value) { int q1 = value.indexOf('\''); if (q1 == -1) { // missing charset return value; } String mimeCharset = value.substring(0, q1); int q2 = value.indexOf('\'', q1 + 1); if (q2 == -1) { // missing language return value; } byte[] bytes = fromHex(value.substring(q2 + 1)); try { return new String(bytes, getJavaCharset(mimeCharset)); } catch (UnsupportedEncodingException e) { // incorrect encoding return value; } }
MimeHelper.encodeContentDisposition(MimeHelper.DISPOSITION_ATTACHMENT, contentFilename)); } else { response.setHeader(MimeHelper.CONTENT_DISPOSITION, MimeHelper.encodeContentDisposition(MimeHelper.DISPOSITION_INLINE, contentFilename));
contentStream.setFileName(MimeHelper.decodeContentDispositionFilename(contentDisposition));
MimeHelper.decodeContentDisposition(contentDisposition, params); boolean isContent = params.containsKey(MimeHelper.DISPOSITION_FILENAME); String fieldCharset = MimeHelper.getCharsetFromContentType(fieldContentType); if (fieldCharset != null) { addField(name, new String(rawValue, fieldCharset));
/** * Gets charset from a content type header. * * @param value * the header value to decode * @return the charset or <code>null</code> if no valid boundary available */ public static String getCharsetFromContentType(String value) { try { HeaderTokenizer tokenizer = new HeaderTokenizer(value, ";", true); // get the first token, which must be an ATOM Token token = tokenizer.next(); if (token.getType() != Token.ATOM) { return null; } // the remainder is the parameters String remainder = tokenizer.getRemainder(); Map<String, String> params; if (remainder != null) { params = new HashMap<String, String>(); getParameters(remainder, params); return params.get("charset"); } } catch (ParseException e) { return null; } return null; }
int respCode = conn.getResponseCode(); if (respCode == 401) { Map<String, Map<String, String>> challenges = MimeHelper.getChallengesFromAuthenticateHeader(conn .getHeaderField("WWW-Authenticate"));
value = decodeRFC2231value(value);
out, "Content-Disposition: " + MimeHelper.encodeContentDisposition(MimeHelper.DISPOSITION_FORM_DATA_CONTENT, filename)); writeLine(out, "Content-Type: " + mediaType); writeLine(out, "Content-Transfer-Encoding: binary");
String contentDisposition = resp.getContentDisposition(); if (contentDisposition != null) { filename = MimeHelper.decodeContentDispositionFilename(contentDisposition);
protected static String decodeRFC2231value(String value) { int q1 = value.indexOf('\''); if (q1 == -1) { // missing charset return value; } String mimeCharset = value.substring(0, q1); int q2 = value.indexOf('\'', q1 + 1); if (q2 == -1) { // missing language return value; } byte[] bytes = fromHex(value.substring(q2 + 1)); try { return new String(bytes, getJavaCharset(mimeCharset)); } catch (UnsupportedEncodingException e) { // incorrect encoding return value; } }
/** * Gets charset from a content type header. * * @param value * the header value to decode * @return the charset or <code>null</code> if no valid boundary available */ public static String getCharsetFromContentType(String value) { try { HeaderTokenizer tokenizer = new HeaderTokenizer(value, ";", true); // get the first token, which must be an ATOM Token token = tokenizer.next(); if (token.getType() != Token.ATOM) { return null; } // the remainder is the parameters String remainder = tokenizer.getRemainder(); Map<String, String> params; if (remainder != null) { params = new HashMap<String, String>(); getParameters(remainder, params); return params.get("charset"); } } catch (ParseException e) { return null; } return null; }
int respCode = conn.getResponseCode(); if (respCode == 401) { Map<String, Map<String, String>> challenges = MimeHelper.getChallengesFromAuthenticateHeader(conn .getHeaderField("WWW-Authenticate"));
value = decodeRFC2231value(value);
out, "Content-Disposition: " + MimeHelper.encodeContentDisposition(MimeHelper.DISPOSITION_FORM_DATA_CONTENT, filename)); writeLine(out, "Content-Type: " + mediaType); writeLine(out, "Content-Transfer-Encoding: binary");
String contentDisposition = resp.getContentDisposition(); if (contentDisposition != null) { filename = MimeHelper.decodeContentDispositionFilename(contentDisposition);
if (remainder != null) { Map<String, String> params = new HashMap<String, String>(); getParameters(remainder, params);
/** * Decodes a filename from the Content-Disposition header value according to * RFC 2183 and RFC 2231. * <p> * See <a href="http://tools.ietf.org/html/rfc2231">RFC 2231</a> for * details. * * @param value * the header value to decode * @return the filename */ public static String decodeContentDispositionFilename(String value) { Map<String, String> params = new HashMap<String, String>(); decodeContentDisposition(value, params); return params.get(DISPOSITION_FILENAME); }