/** * Unescape and decode a given string regarded as an escaped string with the * default protocol charset. * * @param escaped a string * @return the unescaped string * * @throws URIException if the string cannot be decoded (invalid) * * @see URI#getDefaultProtocolCharset */ public static String decode(String escaped) throws URIException { try { byte[] rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(escaped)); return EncodingUtil.getString(rawdata, URI.getDefaultProtocolCharset()); } catch (DecoderException e) { throw new URIException(e.getMessage()); } }
/** * Generates a request entity from the post parameters, if present. Calls * {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set. * * @since 3.0 */ protected RequestEntity generateRequestEntity() { if (!this.params.isEmpty()) { // Use a ByteArrayRequestEntity instead of a StringRequestEntity. // This is to avoid potential encoding issues. Form url encoded strings // are ASCII by definition but the content type may not be. Treating the content // as bytes allows us to keep the current charset without worrying about how // this charset will effect the encoding of the form url encoded string. String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet()); ByteArrayRequestEntity entity = new ByteArrayRequestEntity( EncodingUtil.getAsciiBytes(content), FORM_URL_ENCODED_CONTENT_TYPE ); return entity; } else { return super.generateRequestEntity(); } }
String tmp2=encode(md5Helper.digest(EncodingUtil.getBytes(a1, charset))); StringBuffer tmp3 = new StringBuffer(tmp2.length() + nonce.length() + cnonce.length() + 2); tmp3.append(tmp2); LOG.warn("Unhandled algorithm " + algorithm + " requested"); String md5a1 = encode(md5Helper.digest(EncodingUtil.getBytes(a1, charset))); a2 = method + ":" + uri; String md5a2 = encode(md5Helper.digest(EncodingUtil.getAsciiBytes(a2))); encode(md5Helper.digest(EncodingUtil.getAsciiBytes(serverDigestValue)));
/** * Escape and encode a given string with allowed characters not to be * escaped and a given charset. * * @param unescaped a string * @param allowed allowed characters not to be escaped * @param charset the charset * @return the escaped string */ public static String encode(String unescaped, BitSet allowed, String charset) throws URIException { byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtil.getBytes(unescaped, charset)); return EncodingUtil.getAsciiString(rawdata); }
/** * Writes the cache out onto the underlying stream * @throws IOException * * @since 3.0 */ protected void flushCache() throws IOException { if (cachePosition > 0) { byte chunkHeader[] = EncodingUtil.getAsciiBytes( Integer.toHexString(cachePosition) + "\r\n"); stream.write(chunkHeader, 0, chunkHeader.length); stream.write(cache, 0, cachePosition); stream.write(ENDCHUNK, 0, ENDCHUNK.length); cachePosition = 0; } }
/** * Extracts the server nonce out of the given message type 2. * * @param message the String containing the base64 encoded message. * @return an array of 8 bytes that the server sent to be used when * hashing the password. */ public byte[] parseType2Message(String message) { // Decode the message first. byte[] msg = Base64.decodeBase64(EncodingUtil.getBytes(message, DEFAULT_CHARSET)); byte[] nonce = new byte[8]; // The nonce is the 8 bytes starting from the byte in position 24. for (int i = 0; i < 8; i++) { nonce[i] = msg[i + 24]; } return nonce; }
/** * Sets the query string of this HTTP method. The pairs are encoded as UTF-8 characters. * To use a different charset the parameters can be encoded manually using EncodingUtil * and set as a single String. * * @param params an array of {@link NameValuePair}s to add as query string * parameters. The name/value pairs will be automcatically * URL encoded * * @see EncodingUtil#formUrlEncode(NameValuePair[], String) * @see #setQueryString(String) */ public void setQueryString(NameValuePair[] params) { LOG.trace("enter HttpMethodBase.setQueryString(NameValuePair[])"); queryString = EncodingUtil.formUrlEncode(params, "UTF-8"); }
/** * Converts the byte array of HTTP content characters to a string. If * the specified charset is not supported, default system encoding * is used. * * @param data the byte array to be encoded * @param charset the desired character encoding * @return The result of the conversion. * * @since 3.0 */ public static String getString(final byte[] data, String charset) { return getString(data, 0, data.length, charset); }
/** * Converts the byte array of ASCII characters to a string. This method is * to be used when decoding content of HTTP elements (such as response * headers) * * @param data the byte array to be encoded * @return The string representation of the byte array * * @since 3.0 */ public static String getAsciiString(final byte[] data) { return getAsciiString(data, 0, data.length); }
return doFormUrlEncode(pairs, charset); } catch (UnsupportedEncodingException e) { LOG.error("Encoding not supported: " + charset); try { return doFormUrlEncode(pairs, DEFAULT_CHARSET); } catch (UnsupportedEncodingException fatal) {
/** * Returns a basic <tt>Authorization</tt> header value for the given * {@link UsernamePasswordCredentials} and charset. * * @param credentials The credentials to encode. * @param charset The charset to use for encoding the credentials * * @return a basic authorization string * * @since 3.0 */ public static String authenticate(UsernamePasswordCredentials credentials, String charset) { LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)"); if (credentials == null) { throw new IllegalArgumentException("Credentials may not be null"); } if (charset == null || charset.length() == 0) { throw new IllegalArgumentException("charset may not be null or empty"); } StringBuffer buffer = new StringBuffer(); buffer.append(credentials.getUserName()); buffer.append(":"); buffer.append(credentials.getPassword()); return "Basic " + EncodingUtil.getAsciiString( Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset))); }
/** * Writes the cache and bufferToAppend to the underlying stream * as one large chunk * @param bufferToAppend * @param off * @param len * @throws IOException * * @since 3.0 */ protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException { byte chunkHeader[] = EncodingUtil.getAsciiBytes( Integer.toHexString(cachePosition + len) + "\r\n"); stream.write(chunkHeader, 0, chunkHeader.length); stream.write(cache, 0, cachePosition); stream.write(bufferToAppend, off, len); stream.write(ENDCHUNK, 0, ENDCHUNK.length); cachePosition = 0; }
/** * Gets the content in bytes. Bytes are lazily created to allow the charset to be changed * after the part is created. * * @return the content in bytes */ private byte[] getContent() { if (content == null) { content = EncodingUtil.getBytes(value, getCharSet()); } return content; }
/** * Sets the query string of this HTTP method. The pairs are encoded as UTF-8 characters. * To use a different charset the parameters can be encoded manually using EncodingUtil * and set as a single String. * * @param params an array of {@link NameValuePair}s to add as query string * parameters. The name/value pairs will be automcatically * URL encoded * * @see EncodingUtil#formUrlEncode(NameValuePair[], String) * @see #setQueryString(String) */ public void setQueryString(NameValuePair[] params) { LOG.trace("enter HttpMethodBase.setQueryString(NameValuePair[])"); queryString = EncodingUtil.formUrlEncode(params, "UTF-8"); }
return EncodingUtil.getString(rawdata, getResponseCharSet()); } else { return null;
/** * Returns the response that has been generated after shrinking the array if * required and base64 encodes the response. * @return The response as above. */ private String getResponse() { byte[] resp; if (currentResponse.length > currentPosition) { byte[] tmp = new byte[currentPosition]; for (int i = 0; i < currentPosition; i++) { tmp[i] = currentResponse[i]; } resp = tmp; } else { resp = currentResponse; } return EncodingUtil.getAsciiString(Base64.encodeBase64(resp)); }
return doFormUrlEncode(pairs, charset); } catch (UnsupportedEncodingException e) { LOG.error("Encoding not supported: " + charset); try { return doFormUrlEncode(pairs, DEFAULT_CHARSET); } catch (UnsupportedEncodingException fatal) {
rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(component)); } catch (DecoderException e) { throw new URIException(e.getMessage()); return EncodingUtil.getString(rawdata, charset);
/** * Generates a request entity from the post parameters, if present. Calls * {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set. * * @since 3.0 */ protected RequestEntity generateRequestEntity() { if (!this.params.isEmpty()) { // Use a ByteArrayRequestEntity instead of a StringRequestEntity. // This is to avoid potential encoding issues. Form url encoded strings // are ASCII by definition but the content type may not be. Treating the content // as bytes allows us to keep the current charset without worrying about how // this charset will effect the encoding of the form url encoded string. String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet()); ByteArrayRequestEntity entity = new ByteArrayRequestEntity( EncodingUtil.getAsciiBytes(content), FORM_URL_ENCODED_CONTENT_TYPE ); return entity; } else { return super.generateRequestEntity(); } }
@Override protected void sendDispositionHeader(final OutputStream out) throws IOException { out.write(CONTENT_DISPOSITION_BYTES); out.write(QUOTE_BYTES); out.write(EncodingUtil.getAsciiBytes(getName())); out.write(QUOTE_BYTES); final String filename = getSource().getFileName(); if (filename != null) { out.write(EncodingUtil.getAsciiBytes(FILE_NAME)); out.write(QUOTE_BYTES); out.write(EncodingUtil.getBytes(getFileName(), pageCharset_)); out.write(QUOTE_BYTES); } }