Example usage for org.apache.commons.httpclient.util EncodingUtil getAsciiBytes

List of usage examples for org.apache.commons.httpclient.util EncodingUtil getAsciiBytes

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.util EncodingUtil getAsciiBytes.

Prototype

public static byte[] getAsciiBytes(final String data) 

Source Link

Document

Converts the specified string to byte array of ASCII characters.

Usage

From source file:com.ltasks.GZipPostMethod.java

@Override
protected RequestEntity generateRequestEntity() {
    if (mIsGzip) {
        try {/*w  w w  .  j a v a 2s.  co m*/
            String contentStr = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());

            ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
            originalContent.write(EncodingUtil.getAsciiBytes(contentStr));

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos);
            GZIPOutputStream gzipOut = new GZIPOutputStream(baos);

            originalContent.writeTo(gzipOut);

            gzipOut.finish();
            byte[] content = baos.toByteArray();

            ByteArrayRequestEntity entity = new ByteArrayRequestEntity(content, FORM_URL_ENCODED_CONTENT_TYPE);
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    } else {
        return super.generateRequestEntity();
    }
}

From source file:lucee.commons.net.http.httpclient3.ResourcePart.java

public static void sendDispositionHeader(String name, String filename, String headerCharset, OutputStream out)
        throws IOException {
    out.write(CONTENT_DISPOSITION_BYTES);
    out.write(QUOTE_BYTES);/*  w w  w. j a va 2  s  .  co m*/
    if (StringUtil.isAscii(name))
        out.write(EncodingUtil.getAsciiBytes(name));
    else
        out.write(name.getBytes(headerCharset));
    out.write(QUOTE_BYTES);

    if (filename != null) {
        out.write(FILE_NAME_BYTES);
        out.write(QUOTE_BYTES);
        if (StringUtil.isAscii(filename))
            out.write(EncodingUtil.getAsciiBytes(filename));
        else
            out.write(filename.getBytes(headerCharset));
        out.write(QUOTE_BYTES);
    }
}

From source file:io.s4.example.twittertopiccount.TwitterFeedListener.java

public void connectAndRead() throws Exception {
    URL url = new URL(urlString);

    URLConnection connection = url.openConnection();
    String userPassword = userid + ":" + password;
    String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(userPassword)));
    connection.setRequestProperty("Authorization", "Basic " + encoded);
    connection.connect();// w ww  .jav  a2s  .c o m

    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    String inputLine = null;
    while ((inputLine = br.readLine()) != null) {
        if (inputLine.trim().length() == 0) {
            blankCount++;
            continue;
        }
        messageCount++;
        messageQueue.add(inputLine);
    }
}

From source file:com.cyberway.issue.net.LaxURI.java

protected static String decode(String component, String charset) throws URIException {
    if (component == null) {
        throw new IllegalArgumentException("Component array of chars may not be null");
    }//from w w  w  .  ja  v a 2  s.  co  m
    byte[] rawdata = null;
    //     try {
    rawdata = LaxURLCodec.decodeUrlLoose(EncodingUtil.getAsciiBytes(component));
    //     } catch (DecoderException e) {
    //         throw new URIException(e.getMessage());
    //     }
    return EncodingUtil.getString(rawdata, charset);
}

From source file:io.s4.latin.adapter.TwitterFeedListener.java

public void connectAndRead() throws Exception {
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000);//from  w ww .j  a v  a2s. co  m

    String userPassword = userid + ":" + password;
    System.out.println("connect to " + connection.getURL().toString() + " ...");

    String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(userPassword)));
    connection.setRequestProperty("Authorization", "Basic " + encoded);
    connection.setRequestProperty("Accept-Charset", "utf-8,ISO-8859-1");
    connection.connect();
    System.out.println("Connect OK!");
    System.out.println("Reading TwitterFeed ....");
    Long startTime = new Date().getTime();
    InputStream is = connection.getInputStream();
    Charset utf8 = Charset.forName("UTF-8");

    InputStreamReader isr = new InputStreamReader(is, utf8);
    BufferedReader br = new BufferedReader(isr);

    String inputLine = null;

    while ((inputLine = br.readLine()) != null) {
        if (inputLine.trim().length() == 0) {
            blankCount++;
            continue;
        }
        messageCount++;
        messageQueue.add(inputLine);
        if (messageCount % 500 == 0) {
            Long currentTime = new Date().getTime();
            System.out.println("Lines processed: " + messageCount + "\t ( " + blankCount + " empty lines ) in "
                    + (currentTime - startTime) / 1000 + " seconds. Reading "
                    + messageCount / ((currentTime - startTime) / 1000) + " rows/second");
        }
    }
}

From source file:org.alfresco.repo.transfer.ContentDataPart.java

/**
 * Write the disposition header to the output stream
 * @param out The output stream/*from  ww  w  .jav  a 2 s .c o m*/
 * @throws IOException If an IO problem occurs
 * @see org.apache.commons.httpclient.methods.multipart.Part#sendDispositionHeader(OutputStream)
 */
protected void sendDispositionHeader(OutputStream out) throws IOException {
    super.sendDispositionHeader(out);
    if (filename != null) {
        out.write(FILE_NAME_BYTES);
        out.write(QUOTE_BYTES);
        out.write(EncodingUtil.getAsciiBytes(filename));
        out.write(QUOTE_BYTES);
    }
}

From source file:org.apache.jackrabbit.spi2davex.BinaryPart.java

@Override
protected void sendDispositionHeader(OutputStream out) throws IOException {
    out.write(CONTENT_DISPOSITION_BYTES);
    out.write(QUOTE_BYTES);//from  w  w  w  .j  a  v  a 2s  .  co m
    out.write(EncodingUtil.getBytes(getName(), getCharSet()));
    out.write(QUOTE_BYTES);
    String filename = getSource().getFileName();
    if (filename != null) {
        out.write(EncodingUtil.getAsciiBytes(FILE_NAME));
        out.write(QUOTE_BYTES);
        out.write(EncodingUtil.getBytes(getName(), getCharSet()));
        out.write(QUOTE_BYTES);
    }
}

From source file:org.apache.jackrabbit.spi2davex.PostMethod.java

@Override
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.
        NameValuePair[] mvps = params.toArray(new NameValuePair[params.size()]);
        String content = EncodingUtil.formUrlEncode(mvps, getRequestCharSet());
        ByteArrayRequestEntity entity = new ByteArrayRequestEntity(EncodingUtil.getAsciiBytes(content),
                FORM_URL_ENCODED_CONTENT_TYPE);
        return entity;
    } else {/* ww  w  . j  a  v a2 s  .  com*/
        return super.generateRequestEntity();
    }
}

From source file:org.eclipse.mylyn.internal.bugzilla.core.BugzillaFilePart.java

@Override
protected void sendDispositionHeader(OutputStream out) throws IOException {
    super.sendDispositionHeader(out);
    if (filename != null) {
        out.write(EncodingUtil.getAsciiBytes(FILE_NAME));
        out.write(QUOTE_BYTES);/*www. j av  a  2s  . c o m*/
        out.write(EncodingUtil.getBytes(filename, getCharSet()));
        out.write(QUOTE_BYTES);
    }
}

From source file:org.openxdm.xcap.server.slee.auth.RFC2617AuthQopDigest.java

/**
 * Calculates the encoded http digest according to RFC 2617 with "auth" qop.
 * //from   ww  w .ja  va2 s. c o  m
 * @return
 * @throws NoSuchAlgorithmException
 */
public String digest() throws InternalServerErrorException {

    if (logger.isDebugEnabled()) {
        logger.debug("Calculating RFC 2617 qop=auth digest with params: username = " + username + " , realm = "
                + realm + " , password = " + password + " , nonce = " + nonce + " , nonceCount = " + nonceCount
                + " , cnonce = " + cnonce + " , method = " + method + " , digestUri = " + digestUri + " ;");
    }

    MessageDigest messageDigest = null;
    try {
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new InternalServerErrorException(
                "failed to get instance of MD5 digest, used in " + this.getClass());
    }

    final String a1 = username + ":" + realm + ":" + password;
    final String ha1 = AsciiHexStringEncoder.encode(messageDigest.digest(EncodingUtil.getAsciiBytes(a1)));

    final String a2 = method + ":" + digestUri;
    final String ha2 = AsciiHexStringEncoder.encode(messageDigest.digest(EncodingUtil.getAsciiBytes(a2)));

    final String kd = ha1 + ":" + nonce + ":" + nonceCount + ":" + cnonce + ":" + qop + ":" + ha2;

    return AsciiHexStringEncoder.encode(messageDigest.digest(EncodingUtil.getAsciiBytes(kd)));
}