List of usage examples for org.apache.commons.httpclient.util EncodingUtil getAsciiString
public static String getAsciiString(final byte[] data)
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();//www . ja v a2s . co 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.basho.riak.client.util.Multipart.java
/** * Parses a multipart message or a multipart subpart of a multipart message. * /*from w w w . ja v a 2 s . co m*/ * @return A list of the parts parsed into headers and body of this * multipart message */ public static List<Multipart.Part> parse(Map<String, String> headers, byte[] body) { if (headers == null || body == null || body.length == 0) return null; if (!(body.length >= 2 && body[0] == '\r' && body[1] == '\n')) { // In order to parse the multipart efficiently, we want to treat the // first boundary identically to the others, so make sure that the // first boundary is preceded by a '\r\n' like the others byte[] newBody = new byte[body.length + 2]; newBody[0] = '\r'; newBody[1] = '\n'; System.arraycopy(body, 0, newBody, 2, body.length); body = newBody; } String boundary = "\r\n--" + getBoundary(headers.get(Constants.HDR_CONTENT_TYPE)); byte[] boundaryBytes = boundary.getBytes(); int boundarySize = boundary.length(); if ("\r\n--".equals(boundary)) return null; // While this parsing could be more efficiently done in one pass with a // hand written FSM, hopefully this method is more readable/intuitive. List<Part> parts = new ArrayList<Part>(); int pos = indexOf(body, boundaryBytes, 0); if (pos != -1) { while (pos < body.length) { // first char of part int start = pos + boundarySize; // last char of part + 1 int end = indexOf(body, boundaryBytes, start); // end of header section + 1 int headerEnd = indexOf(body, HEADER_DELIM, pos); // start of body section int bodyStart = headerEnd + HEADER_DELIM.length; // check for end boundary, which is (boundary + "--") if (body.length >= (start + 2) && body[start] == '-' && body[start + 1] == '-') { break; } if (end == -1) { end = body.length; } if (headerEnd == -1) { headerEnd = body.length; bodyStart = end; } if (bodyStart > end) { bodyStart = end; } Map<String, String> partHeaders = parseHeaders( EncodingUtil.getAsciiString(copyOfRange(body, start, headerEnd))); parts.add(new Part(partHeaders, copyOfRange(body, bodyStart, end))); pos = end; } } return parts; }
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);/*www. j a v a2s . c om*/ 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:davmail.http.SpNegoScheme.java
/** * Produces Negotiate authorization string for the given set of * {@link Credentials}./*from ww w . ja v a 2 s .co m*/ * * @param credentials The set of credentials to be used for authentication * @param httpMethod The method being authenticated * @return an Negotiate authorization string * @throws org.apache.commons.httpclient.auth.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 */ public String authenticate(Credentials credentials, HttpMethod httpMethod) throws AuthenticationException { if (this.state == UNINITIATED) { throw new IllegalStateException("Negotiate authentication process has not been initiated"); } String host = null; try { host = httpMethod.getURI().getHost(); } catch (URIException e) { // ignore } if (host == null) { Header header = httpMethod.getRequestHeader("Host"); if (header != null) { host = header.getValue(); if (host.indexOf(':') >= 0) { host = host.substring(0, host.indexOf(':')); } } } if (host == null) { throw new IllegalStateException("Negotiate authentication failed: empty host"); } // no credentials needed String response; try { if (this.state == INITIATED || this.state == FAILED) { // send initial token to server response = EncodingUtil.getAsciiString( Base64.encodeBase64(KerberosHelper.initSecurityContext("HTTP", host, new byte[0]))); this.state = TYPE1_MSG_GENERATED; } else { // send challenge response response = EncodingUtil.getAsciiString( Base64.encodeBase64(KerberosHelper.initSecurityContext("HTTP", host, serverToken))); this.state = TYPE3_MSG_GENERATED; } } catch (GSSException gsse) { state = FAILED; if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL || gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED) throw new InvalidCredentialsException(gsse.getMessage(), gsse); if (gsse.getMajor() == GSSException.NO_CRED) throw new CredentialsNotAvailableException(gsse.getMessage(), gsse); if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN || gsse.getMajor() == GSSException.DUPLICATE_TOKEN || gsse.getMajor() == GSSException.OLD_TOKEN) throw new AuthChallengeException(gsse.getMessage(), gsse); // other error throw new AuthenticationException(gsse.getMessage(), gsse); } catch (LoginException e) { state = FAILED; throw new InvalidCredentialsException(e.getMessage(), e); } return "Negotiate " + response; }
From source file:davmail.http.NTLMv2Scheme.java
/** * Produces NTLM authorization string for the given set of * {@link Credentials}.//w w w . j a va2 s. c o m * * @param credentials The set of credentials to be used for authentication * @param httpMethod The method being authenticated * @return an NTLM authorization string * @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 */ public String authenticate(Credentials credentials, HttpMethod httpMethod) throws AuthenticationException { if (this.state == UNINITIATED) { throw new IllegalStateException("NTLM authentication process has not been initiated"); } NTCredentials ntcredentials; try { ntcredentials = (NTCredentials) credentials; } catch (ClassCastException e) { throw new InvalidCredentialsException( "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName()); } String response; if (this.state == INITIATED || this.state == FAILED) { int flags = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NtlmFlags.NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED | NtlmFlags.NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM | NtlmFlags.NTLMSSP_REQUEST_TARGET | NtlmFlags.NTLMSSP_NEGOTIATE_OEM | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE | NtlmFlags.NTLMSSP_NEGOTIATE_56 | NtlmFlags.NTLMSSP_NEGOTIATE_128; Type1Message type1Message = new Type1Message(flags, ntcredentials.getDomain(), ntcredentials.getHost()); response = EncodingUtil.getAsciiString(Base64.encodeBase64(type1Message.toByteArray())); this.state = TYPE1_MSG_GENERATED; } else { int flags = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE; Type3Message type3Message = new Type3Message(type2Message, ntcredentials.getPassword(), ntcredentials.getDomain(), ntcredentials.getUserName(), ntcredentials.getHost(), flags); response = EncodingUtil.getAsciiString(Base64.encodeBase64(type3Message.toByteArray())); this.state = TYPE3_MSG_GENERATED; } return "NTLM " + response; }
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);//ww w . j a va2s .c o m if (logger.isDebugEnabled()) { System.setProperty("javax.net.debug", "all"); } super.setUp(); }
From source file:org.wso2.carbon.mediator.ntlm.CustomNTLMAuthScheme.java
/** * Produces NTLM authorization string for the given set of * {@link Credentials}.//ww w . j a va2 s . co m * * @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; }