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

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

Introduction

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

Prototype

public static byte[] getBytes(final String data, String charset) 

Source Link

Document

Converts the specified string to a byte array.

Usage

From source file:davmail.http.SpNegoScheme.java

/**
 * Processes the Negotiate challenge.//from w w w. java 2 s. co  m
 *
 * @param challenge the challenge string
 * @throws MalformedChallengeException is thrown if the authentication challenge is malformed
 */
public void processChallenge(final String challenge) throws MalformedChallengeException {
    String authScheme = AuthChallengeParser.extractScheme(challenge);
    if (!authScheme.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid Negotiate challenge: " + challenge);
    }
    int spaceIndex = challenge.indexOf(' ');
    if (spaceIndex != -1) {
        // step 2: received server challenge
        serverToken = Base64.decodeBase64(
                EncodingUtil.getBytes(challenge.substring(spaceIndex, challenge.length()).trim(), "ASCII"));
        this.state = TYPE2_MSG_RECEIVED;
    } else {
        this.serverToken = null;
        if (this.state == UNINITIATED) {
            this.state = INITIATED;
        } else {
            this.state = FAILED;
        }
    }
}

From source file:davmail.http.NTLMv2Scheme.java

/**
 * Processes the NTLM challenge./*from   w w  w.  j  a v  a 2s. com*/
 *
 * @param challenge the challenge string
 * @throws MalformedChallengeException is thrown if the authentication challenge
 *                                     is malformed
 */
public void processChallenge(final String challenge) throws MalformedChallengeException {
    String authScheme = AuthChallengeParser.extractScheme(challenge);
    if (!authScheme.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge);
    }
    int spaceIndex = challenge.indexOf(' ');
    if (spaceIndex != -1) {
        try {
            type2Message = new Type2Message(Base64.decodeBase64(EncodingUtil
                    .getBytes(challenge.substring(spaceIndex, challenge.length()).trim(), "ASCII")));
        } catch (IOException e) {
            throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge, e);
        }
        this.state = TYPE2_MSG_RECEIVED;
    } else {
        this.type2Message = null;
        if (this.state == UNINITIATED) {
            this.state = INITIATED;
        } else {
            this.state = FAILED;
        }
    }
}

From source file:org.apache.axis2.transport.http.util.ComplexPart.java

/**
 * Gets the content in bytes.  Bytes are lazily created to allow the charset to be changed
 * after the part is created./*from  www  .  java2  s . co  m*/
 *
 * @return the content in bytes
 */
private byte[] getContent() {
    if (content == null) {
        content = EncodingUtil.getBytes(value, getCharSet());
    }
    return content;
}

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  ww .  j av  a2  s .c  o  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.StringPart.java

@Override
protected void sendDispositionHeader(OutputStream out) throws IOException {
    out.write(CONTENT_DISPOSITION_BYTES);
    out.write(QUOTE_BYTES);//ww w  .j ava2 s  .co  m
    out.write(EncodingUtil.getBytes(getName(), getCharSet()));
    out.write(QUOTE_BYTES);
}

From source file:org.apache.servicemix.http.HttpSpringTest.java

protected void setUp() throws Exception {
    String str = "Basic "
            + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getBytes("smx:smx", "UTF-8")));
    System.err.println(str);//from  w ww  .  j a v a  2  s .c o m
    if (logger.isDebugEnabled()) {
        System.setProperty("javax.net.debug", "all");
    }
    super.setUp();
}

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);//from w ww. j a  v a 2s.c  om
        out.write(EncodingUtil.getBytes(filename, getCharSet()));
        out.write(QUOTE_BYTES);
    }
}

From source file:org.wso2.carbon.mediator.ntlm.CustomNTLMAuthScheme.java

/**
 * Produces NTLM authorization string for the given set of
 * {@link Credentials}.//from  ww  w.j a va  2s  . c  om
 *
 * @param credentials
 *            The set of credentials to be used for athentication
 * @param method
 *            The method being authenticated
 *
 * @throws InvalidCredentialsException
 *             if authentication credentials are not valid or not applicable
 *             for this authentication scheme
 * @throws AuthenticationException
 *             if authorization string cannot be generated due to an
 *             authentication failure
 *
 * @return an NTLM authorization string
 *
 * @since 3.0
 */
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
    LOG.trace("enter NTLMScheme.authenticate (Credentials, HttpMethod)");

    if (this.state == UNINITIATED) {
        throw new IllegalStateException("NTLM authentication process has not been initiated");
    }

    NTCredentials ntcredentials = null;
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException(
                "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }
    byte[] msgBytes = null;
    String response = null;
    if (this.state == INITIATED) {
        Type1Message msg = new Type1Message();
        // @see http://davenport.sourceforge.net/ntlm.html#theType1Message
        // dont' support Unicode
        // negotiate OEM
        // request authentication realm in Type2 response
        // not signed
        // not encrypted
        // not authenticated
        // no lan manager key
        // negotiate NTLM
        msg.setFlags(0x5206);
        msg.setSuppliedWorkstation(ntcredentials.getHost());
        msg.setSuppliedDomain(ntcredentials.getDomain());
        msgBytes = msg.toByteArray();
        this.state = TYPE1_MSG_GENERATED;
    } else if (this.state == TYPE2_MSG_RECEIVED) {
        byte[] msg2Bytes = Base64.decodeBase64(
                EncodingUtil.getBytes(this.ntlmChallenge, method.getParams().getCredentialCharset()));
        try {
            Type2Message msg2 = new Type2Message(msg2Bytes);
            int flags = Type3Message.NTLMSSP_NEGOTIATE_OEM | Type3Message.NTLMSSP_NEGOTIATE_LM_KEY;

            Type3Message msg3 = new Type3Message(msg2, ntcredentials.getPassword(), ntcredentials.getDomain(),
                    ntcredentials.getUserName(), ntcredentials.getHost(), flags);

            msgBytes = msg3.toByteArray();
        } catch (IOException ex) {
            throw new AuthenticationException("unable to parse Type2Message", ex);
        }
        this.state = TYPE3_MSG_GENERATED;
    } else {
        throw new RuntimeException("failed to authenticate");
    }
    response = EncodingUtil.getAsciiString(Base64.encodeBase64(msgBytes));
    return "NTLM " + response;
}