Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 56815 invoked from network); 18 Aug 2007 00:04:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Aug 2007 00:04:10 -0000 Received: (qmail 14724 invoked by uid 500); 18 Aug 2007 00:04:07 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 14686 invoked by uid 500); 18 Aug 2007 00:04:07 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 14675 invoked by uid 99); 18 Aug 2007 00:04:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Aug 2007 17:04:07 -0700 X-ASF-Spam-Status: No, hits=-98.8 required=10.0 tests=ALL_TRUSTED,DNS_FROM_DOB,RCVD_IN_DOB X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 18 Aug 2007 00:04:04 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5B02E1A981A; Fri, 17 Aug 2007 17:03:44 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r567184 [1/2] - in /geronimo/sandbox/AsyncHttpClient: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/ahc/ src/main/java/org/apache/ahc/codec/ src/main/java/org/apache/ahc/codec/protoco... Date: Sat, 18 Aug 2007 00:03:43 -0000 To: scm@geronimo.apache.org From: jgenender@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070818000344.5B02E1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jgenender Date: Fri Aug 17 17:03:37 2007 New Revision: 567184 URL: http://svn.apache.org/viewvc?view=rev&rev=567184 Log: Adding AsyncHttpClient Added: geronimo/sandbox/AsyncHttpClient/ geronimo/sandbox/AsyncHttpClient/pom.xml geronimo/sandbox/AsyncHttpClient/src/ geronimo/sandbox/AsyncHttpClient/src/main/ geronimo/sandbox/AsyncHttpClient/src/main/java/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AHCCallback.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpConnection.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/HttpIoHandler.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Cookie.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Header.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpProtocolCodecFactory.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/ssl/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/ssl/TrustManagerFactoryImpl.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/AsyncHttpClientException.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateParseException.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateUtil.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/EncodingUtil.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/LangUtils.java geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/NameValuePair.java geronimo/sandbox/AsyncHttpClient/src/test/ geronimo/sandbox/AsyncHttpClient/src/test/catalina/ geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/ geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/ geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/conf/ geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/conf/keystore (with props) geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/conf/web.xml geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/webapps/ geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/webapps/ROOT/ geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/webapps/ROOT/index.jsp geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/webapps/ROOT/pwrd_apache.gif (with props) geronimo/sandbox/AsyncHttpClient/src/test/java/ geronimo/sandbox/AsyncHttpClient/src/test/java/org/ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AbstractTest.java geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AsyncHttpConnectionTest.java Added: geronimo/sandbox/AsyncHttpClient/pom.xml URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/pom.xml?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/pom.xml (added) +++ geronimo/sandbox/AsyncHttpClient/pom.xml Fri Aug 17 17:03:37 2007 @@ -0,0 +1,135 @@ + + + + 4.0.0 + org.apache.ahc + ahc + jar + 1.0-SNAPSHOT + Async Http Client + + + junit + junit + 3.8.1 + test + + + commons-codec + commons-codec + 1.3 + + + org.apache.mina + mina-core + 1.1.1 + + + org.apache.mina + mina-filter-ssl + 1.1.1 + + + org.slf4j + slf4j-simple + 1.4.2 + + + org.apache.tomcat + catalina + 6.0.13 + test + + + org.apache.tomcat + coyote + 6.0.13 + test + + + org.apache.tomcat + servlet-api + 6.0.13 + test + + + org.apache.tomcat.extras + juli-adapters + 6.0.13 + test + + + org.apache.tomcat + jasper + 6.0.13 + test + + + org.apache.tomcat + jasper-jdt + 6.0.13 + test + + + commons-codec + commons-codec + 1.3 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + pertest + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-antrun-plugin + + + process-test-resources + + run + + + + + + rcopy todir="target/var/catalina/conf" file="src/test/catalina/var/catalina/conf/keystore" /> + + + + + + + + + + Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AHCCallback.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AHCCallback.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AHCCallback.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AHCCallback.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc; + +import org.apache.ahc.codec.HttpResponseMessage; + +public interface AHCCallback { + public void onResponse(HttpResponseMessage message); + public void onException(Throwable cause); + public void onClosed(); +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpConnection.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpConnection.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpConnection.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpConnection.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc; + +import org.apache.mina.transport.socket.nio.SocketConnector; +import org.apache.mina.common.IoConnectorConfig; +import org.apache.mina.common.ConnectFuture; +import org.apache.mina.common.IoSession; +import org.apache.mina.filter.SSLFilter; +import org.apache.mina.filter.codec.ProtocolCodecFilter; +import org.apache.ahc.ssl.TrustManagerFactoryImpl; +import org.apache.ahc.codec.HttpRequestMessage; +import org.apache.ahc.codec.HttpProtocolCodecFactory; + +import javax.net.ssl.SSLContext; +import java.net.URL; +import java.net.InetSocketAddress; +import java.security.GeneralSecurityException; +import java.io.IOException; + +public class AsyncHttpConnection { + + public static int DEFAULT_CONNECTION_TIMEOUT = 30; + public static String DEFAULT_SSL_PROTOCOL = "TLS"; + + private URL url; + private boolean followRedirects = true; + private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; + private String sslProtocol = DEFAULT_SSL_PROTOCOL; + private SocketConnector connector; + private IoSession session; + private AHCCallback callback; + + public AsyncHttpConnection(URL url, AHCCallback callback) { + this.url = url; + this.callback = callback; + } + + public void connect() throws Exception { + SocketConnector connector = new SocketConnector(); + IoConnectorConfig cfg = connector.getDefaultConfig(); + + cfg.setConnectTimeout(connectionTimeout); + + String scheme = url.getProtocol(); + int port = url.getPort(); + if (scheme.toLowerCase().equals("https")) { + SSLFilter filter = new SSLFilter(createClientSSLContext()); + filter.setUseClientMode(true); + cfg.getFilterChain().addLast("SSL", filter); + if (port == -1) { + port = 443; + } + } + if (scheme.toLowerCase().equals("http") && (port == -1)) { + port = 80; + } + + cfg.getFilterChain().addLast("protocolFilter",new ProtocolCodecFilter( new HttpProtocolCodecFactory(url))); + + ConnectFuture future = connector.connect(new InetSocketAddress(url.getHost(), port), new HttpIoHandler(callback)); + future.join(); + if (!future.isConnected()) { + throw new IOException("Cannot connect to " + url.toString()); + } + session = future.getSession(); + } + + public void disconnect() { + if (session != null && session.isConnected()) { + session.close(); + } + session = null; + } + + public void sendRequest(HttpRequestMessage message){ + session.write(message); + } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } + + public boolean isFollowRedirects() { + return followRedirects; + } + + public void setFollowRedirects(boolean followRedirects) { + this.followRedirects = followRedirects; + } + + public String getSslProtocol() { + return sslProtocol; + } + + public void setSslProtocol(String sslProtocol) { + this.sslProtocol = sslProtocol; + } + + private SSLContext createClientSSLContext() throws GeneralSecurityException { + SSLContext context = SSLContext.getInstance(sslProtocol); + context.init(null, TrustManagerFactoryImpl.X509_MANAGERS, null); + return context; + } + +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/HttpIoHandler.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/HttpIoHandler.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/HttpIoHandler.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/HttpIoHandler.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc; + +import org.apache.mina.common.IoHandlerAdapter; +import org.apache.mina.common.IoSession; +import org.apache.ahc.codec.HttpResponseMessage; + +public class HttpIoHandler extends IoHandlerAdapter { + private AHCCallback callback; + + + public HttpIoHandler(AHCCallback callback) { + this.callback = callback; + } + + public void messageReceived(IoSession ioSession, Object object) throws Exception { + HttpResponseMessage message = (HttpResponseMessage)object; + callback.onResponse(message); + } + + public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception { + callback.onException(throwable); + } + + + public void sessionClosed(IoSession ioSession) throws Exception { + callback.onClosed(); + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Cookie.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Cookie.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Cookie.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Cookie.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +import java.util.Date; + +public class Cookie { + + private String comment; + private String domain; + private String name; + private String value; + private String path; + private boolean secure; + private int version = 0; + private Date expires; + + public Cookie(String name, String value) { + this.name = name; + this.value = value; + } + + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public boolean isSecure() { + return secure; + } + + public void setSecure(boolean secure) { + this.secure = secure; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + + public Date getExpires() { + return expires; + } + + public void setExpires(Date expires) { + this.expires = expires; + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Header.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Header.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Header.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/Header.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +public class Header { + private String name; + private String value; + + public Header() { + } + + public Header(String name, String value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +import java.util.HashMap; +import java.util.Map; +import java.util.ArrayList; +import java.util.List; + +public class HttpMessage { + + protected List
headers = new ArrayList
(); + protected List cookies = new ArrayList(); + protected Map attributes = new HashMap(); + protected String contentType; + protected int contentLength; + protected byte[] content = null; + + public String getStringContent(){ + if (content == null) + return null; + + return new String(content); + } + + public byte[] getContent() { + return content; + } + + public void setContent(byte[] content) { + this.content = content; + } + + public List getCookies() { + return cookies; + } + + public void setCookies(List cookies) { + this.cookies = cookies; + } + + public void addCookie(Cookie cookie){ + this.cookies.add(cookie); + } + + public String getAttribute(String name){ + return attributes.get(name); + } + + public Map getAttributes() { + return attributes; + } + + public List
getHeaders() { + return headers; + } + + public void setHeaders(List
headers) { + this.headers = headers; + } + + public void addHeader(Header header){ + headers.add(header); + } + + public void addHeader(String name, String value){ + headers.add(new Header(name,value)); + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public int getContentLength() { + return contentLength; + } + + public void setContentLength(int contentLength) { + this.contentLength = contentLength; + } + +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpProtocolCodecFactory.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpProtocolCodecFactory.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpProtocolCodecFactory.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpProtocolCodecFactory.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +import org.apache.mina.filter.codec.ProtocolCodecFactory; +import org.apache.mina.filter.codec.ProtocolEncoder; +import org.apache.mina.filter.codec.ProtocolDecoder; + +import java.net.URL; + +public class HttpProtocolCodecFactory implements ProtocolCodecFactory { + + private final ProtocolEncoder encoder; + private final ProtocolDecoder decoder; + + public HttpProtocolCodecFactory(URL url) { + encoder = new HttpRequestEncoder(url); + decoder = new HttpResponseDecoder(); + } + + public ProtocolEncoder getEncoder() throws Exception { + return encoder; + } + + public ProtocolDecoder getDecoder() throws Exception { + return decoder; + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +import org.apache.mina.filter.codec.demux.MessageEncoder; +import org.apache.mina.filter.codec.ProtocolEncoderOutput; +import org.apache.mina.filter.codec.ProtocolEncoder; +import org.apache.mina.filter.codec.ProtocolEncoderAdapter; +import org.apache.mina.common.IoSession; +import org.apache.mina.common.ByteBuffer; + +import java.util.Set; +import java.util.HashSet; +import java.util.Collections; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.net.URL; + +public class HttpRequestEncoder extends ProtocolEncoderAdapter { + private static final Set TYPES; + private static final byte[] CRLF = new byte[]{0x0D, 0x0A}; + private URL url; + + static { + Set types = new HashSet(); + types.add(HttpRequestMessage.class); + TYPES = Collections.unmodifiableSet(types); + } + + public HttpRequestEncoder(URL url) { + this.url = url; + } + + public Set> getMessageTypes() { + return TYPES; + } + + public void encode(IoSession ioSession, Object message, ProtocolEncoderOutput out) throws Exception { + HttpRequestMessage msg = (HttpRequestMessage) message; + + ByteBuffer buf = ByteBuffer.allocate(256); + // Enable auto-expand for easier encoding + buf.setAutoExpand(true); + + try { + // output all headers except the content length + CharsetEncoder encoder = Charset.defaultCharset().newEncoder(); + buf.putString(msg.getRequestMethod(), encoder); + buf.putString(" ", encoder); + buf.putString(msg.getPath(), encoder); + buf.putString(" HTTP/1.1", encoder); + buf.put(CRLF); + buf.putString("Host: ", encoder); + buf.putString(url.getHost(), encoder); + buf.putString(":", encoder); + buf.putString(url.getPort() + "", encoder); + buf.put(CRLF); + buf.put(CRLF); + } catch (CharacterCodingException ex) { + ex.printStackTrace(); + } + + buf.flip(); + out.write(buf); + + } + +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +import java.net.ProtocolException; +import java.util.Map; +import java.util.HashMap; + +public class HttpRequestMessage extends HttpMessage{ + + public static String REQUEST_GET = "GET"; + public static String REQUEST_POST = "POST"; + public static String REQUEST_HEAD = "HEAD"; + public static String REQUEST_OPTIONS = "OPTIONS"; + public static String REQUEST_PUT = "PUT"; + public static String REQUEST_DELETE = "DELETE"; + public static String REQUEST_TRACE = "TRACE"; + + private String requestMethod = REQUEST_GET; + private String path; + + + public HttpRequestMessage(String path) { + this.path = path; + } + + public String getRequestMethod() { + return requestMethod; + } + + + public void setRequestMethod(String requestMethod) throws ProtocolException { + if (requestMethod.equals(REQUEST_GET) || + requestMethod.equals(REQUEST_POST) || + requestMethod.equals(REQUEST_HEAD) || + requestMethod.equals(REQUEST_OPTIONS) || + requestMethod.equals(REQUEST_PUT) || + requestMethod.equals(REQUEST_DELETE) || + requestMethod.equals(REQUEST_TRACE)) { + this.requestMethod = requestMethod; + return; + } + + throw new ProtocolException("Invalid request method type."); + } + + public void setAttributes(Map attributes) { + this.attributes.putAll(attributes); + } + + public void setATtributes(String name, String value){ + attributes.put(name, value); + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + if (path == null || path.trim().length() == 0) + path = "/"; + this.path = path; + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +import org.apache.ahc.codec.protocol.HttpDecoder; +import org.apache.mina.common.ByteBuffer; +import org.apache.mina.common.IoSession; +import org.apache.mina.filter.codec.CumulativeProtocolDecoder; +import org.apache.mina.filter.codec.ProtocolDecoderOutput; + +public class HttpResponseDecoder extends CumulativeProtocolDecoder { + + private HttpDecoder httpDecoder = new HttpDecoder(); + private HttpResponseMessage response = new HttpResponseMessage(); + private boolean headersRead = false; + + protected boolean doDecode(IoSession ioSession, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { + + //Test if we need the response... + if (response.getStatusCode() == 0) { + //Read the status header + String header = httpDecoder.decodeLine(in); + if (header == null) + return false; + + httpDecoder.decodeStatus(header, response); + } + + //Have we read the headers yet? + if (!headersRead) { + //Read the headers and process them + while (true) { + String line = httpDecoder.decodeLine(in); + + //Check if the entire response has been read + if (line == null) + return false; + + //Check if the entire response headers have been read + if (line.length() == 0) { + headersRead = true; + break; + } + + httpDecoder.decodeHeader(line, response); + } + } + + if (response.getContentLength() > 0) { + //Be sure all of the data is there for us to retrieve... + if ((response.getContentLength()) > in.remaining()) + return false; + httpDecoder.decodeContent(in, response); + } + + out.write(response); + + return true; + } + +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec; + +public class HttpResponseMessage extends HttpMessage { + + private int statusCode; + private String statusMessage; + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + public String getStatusMessage() { + return statusMessage; + } + + public void setStatusMessage(String statusMessage) { + this.statusMessage = statusMessage; + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.codec.protocol; + +import org.apache.ahc.codec.HttpResponseMessage; +import org.apache.ahc.codec.Cookie; +import org.apache.ahc.util.DateUtil; +import org.apache.mina.common.ByteBuffer; + +import java.io.IOException; +import java.util.Date; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.Charset; + +public class HttpDecoder { + /** + * Carriage return character + */ + private static final byte CR = 13; + + /** + * Line feed character + */ + private static final byte LF = 10; + + public final static String CONTENT_TYPE = "Content-Type"; + public final static String CONTENT_LENGTH = "Content-Length"; + public final static String SET_COOKIE = "Set-Cookie"; + + public final static String COOKIE_COMMENT = "comment"; + public final static String COOKIE_DOMAIN = "domain"; + public final static String COOKIE_EXPIRES = "expires"; + public final static String COOKIE_MAX_AGE = "max-age"; + public final static String COOKIE_PATH = "path"; + public final static String COOKIE_SECURE = "secure"; + public final static String COOKIE_VERSION = "version"; + + private CharsetDecoder decoder = Charset.defaultCharset().newDecoder(); + + public String decodeLine(ByteBuffer in) throws Exception { + int beginPos = in.position(); + int limit = in.limit(); + boolean lastIsCR = false; + int terminatorPos = -1; + + for (int i = beginPos; i < limit; i++) { + byte b = in.get(i); + if (b == CR) { + lastIsCR = true; + } else { + if (b == LF && lastIsCR) { + terminatorPos = i; + break; + } + lastIsCR = false; + } + } + + //Check if we don't have enough data to process or found a full readable line + if (terminatorPos == -1) + return null; + + String result = null; + if (terminatorPos > 1) { + ByteBuffer line = in.slice(); + line.limit(terminatorPos - beginPos - 1); + result = line.getString(decoder); + } + + in.position(terminatorPos + 1); + + return result; + } + + public void decodeStatus(String line, HttpResponseMessage msg) throws Exception { + String magic = line.substring(0, 8); + if (!magic.equals("HTTP/1.1")) + throw new IOException("Invalid HTTP response"); + + String status = line.substring(9, 12); + msg.setStatusCode(Integer.parseInt(status)); + msg.setStatusMessage(line.substring(13)); + } + + public void decodeHeader(String line, HttpResponseMessage msg) throws Exception { + int pos = line.indexOf(": "); + String name = line.substring(0, pos); + String value = line.substring(pos + 2); + msg.addHeader(name, value); + + if (name.equalsIgnoreCase(SET_COOKIE)) { + Cookie cookie = decodeCookie(value); + if (cookie != null) msg.addCookie(cookie); + } + + if (name.equalsIgnoreCase(CONTENT_TYPE)) { + msg.setContentType(value); + } + + if (name.equalsIgnoreCase(CONTENT_LENGTH)) { + msg.setContentLength(Integer.parseInt(value)); + } + + } + + public void decodeContent(ByteBuffer in, HttpResponseMessage msg){ + byte content[] = new byte[msg.getContentLength()]; + in.get(content); + msg.setContent(content); + } + + public Cookie decodeCookie(String cookieStr) throws Exception { + + Cookie cookie = null; + + String pairs[] = cookieStr.split(";"); + for (int i = 0; i < pairs.length; i++) { + String nameValue[] = pairs[i].trim().split("="); + String name = nameValue[0].trim(); + + //First one is the cookie name/value pair + if (i == 0) { + cookie = new Cookie(name, nameValue[1].trim()); + continue; + } + + if (name.equalsIgnoreCase(COOKIE_COMMENT)) { + cookie.setComment(nameValue[1].trim()); + continue; + } + + if (name.equalsIgnoreCase(COOKIE_PATH)) { + cookie.setPath(nameValue[1].trim()); + } + + if (name.equalsIgnoreCase(COOKIE_SECURE)) { + cookie.setSecure(true); + } + + if (name.equalsIgnoreCase(COOKIE_VERSION)) { + cookie.setVersion(Integer.parseInt(nameValue[1])); + } + + if (name.equalsIgnoreCase(COOKIE_MAX_AGE)) { + int age = Integer.parseInt(nameValue[1]); + cookie.setExpires(new Date(System.currentTimeMillis() + age * 1000L)); + } + + if (name.equalsIgnoreCase(COOKIE_EXPIRES)) { + cookie.setExpires(DateUtil.parseDate(nameValue[1])); + } + + } + + return cookie; + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/ssl/TrustManagerFactoryImpl.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/ssl/TrustManagerFactoryImpl.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/ssl/TrustManagerFactoryImpl.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/ssl/TrustManagerFactoryImpl.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.ssl; + +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactorySpi; +import javax.net.ssl.X509TrustManager; +import javax.net.ssl.ManagerFactoryParameters; +import java.security.cert.X509Certificate; +import java.security.cert.CertificateException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.InvalidAlgorithmParameterException; + +public class TrustManagerFactoryImpl extends TrustManagerFactorySpi { + static final X509TrustManager X509 = new X509TrustManager() { + + + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + + } + + + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + + } + + + public X509Certificate[] getAcceptedIssuers() { + + return new X509Certificate[0]; + + } + + }; + + public static final TrustManager[] X509_MANAGERS = new TrustManager[]{X509}; + + + protected void engineInit(KeyStore keyStore) throws KeyStoreException { + } + + protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException { + } + + protected TrustManager[] engineGetTrustManagers() { + return X509_MANAGERS; + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/AsyncHttpClientException.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/AsyncHttpClientException.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/AsyncHttpClientException.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/AsyncHttpClientException.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.util; + +public class AsyncHttpClientException extends Error{ + + public AsyncHttpClientException() { + } + + public AsyncHttpClientException(String string) { + super(string); + } + + public AsyncHttpClientException(String string, Throwable throwable) { + super(string, throwable); + } + + public AsyncHttpClientException(Throwable throwable) { + super(throwable); + } +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateParseException.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateParseException.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateParseException.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateParseException.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.util; + +/** + * An exception to indicate an error parsing a date string. + * + * @see DateUtil + * + * @author Michael Becke + */ +public class DateParseException extends Exception { + + /** + * + */ + public DateParseException() { + super(); + } + + /** + * @param message the exception message + */ + public DateParseException(String message) { + super(message); + } + +} + Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateUtil.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateUtil.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateUtil.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/DateUtil.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,199 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.Locale; +import java.util.TimeZone; + +/** + * A utility class for parsing and formatting HTTP dates as used in cookies and + * other headers. This class handles dates as defined by RFC 2616 section + * 3.3.1 as well as some other common non-standard formats. + * + * @author Christopher Brown + * @author Michael Becke + */ +public class DateUtil { + + /** + * Date format pattern used to parse HTTP date headers in RFC 1123 format. + */ + public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz"; + + /** + * Date format pattern used to parse HTTP date headers in RFC 1036 format. + */ + public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz"; + + /** + * Date format pattern used to parse HTTP date headers in ANSI C + * asctime() format. + */ + public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; + + private static final Collection DEFAULT_PATTERNS = Arrays.asList( + new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } ); + + private static final Date DEFAULT_TWO_DIGIT_YEAR_START; + + static { + Calendar calendar = Calendar.getInstance(); + calendar.set(2000, Calendar.JANUARY, 1, 0, 0); + DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); + } + + private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); + + /** + * Parses a date value. The formats used for parsing the date value are retrieved from + * the default http params. + * + * @param dateValue the date value to parse + * + * @return the parsed date + * + * @throws DateParseException if the value could not be parsed using any of the + * supported date formats + */ + public static Date parseDate(String dateValue) throws DateParseException { + return parseDate(dateValue, null, null); + } + + /** + * Parses the date value using the given date formats. + * + * @param dateValue the date value to parse + * @param dateFormats the date formats to use + * + * @return the parsed date + * + * @throws DateParseException if none of the dataFormats could parse the dateValue + */ + public static Date parseDate(String dateValue, Collection dateFormats) + throws DateParseException { + return parseDate(dateValue, dateFormats, null); + } + + /** + * Parses the date value using the given date formats. + * + * @param dateValue the date value to parse + * @param dateFormats the date formats to use + * @param startDate During parsing, two digit years will be placed in the range + * startDate to startDate + 100 years. This value may + * be null. When null is given as a parameter, year + * 2000 will be used. + * + * @return the parsed date + * + * @throws DateParseException if none of the dataFormats could parse the dateValue + */ + public static Date parseDate( + String dateValue, + Collection dateFormats, + Date startDate + ) throws DateParseException { + + if (dateValue == null) { + throw new IllegalArgumentException("dateValue is null"); + } + if (dateFormats == null) { + dateFormats = DEFAULT_PATTERNS; + } + if (startDate == null) { + startDate = DEFAULT_TWO_DIGIT_YEAR_START; + } + // trim single quotes around date if present + // see issue #5279 + if (dateValue.length() > 1 + && dateValue.startsWith("'") + && dateValue.endsWith("'") + ) { + dateValue = dateValue.substring (1, dateValue.length() - 1); + } + + SimpleDateFormat dateParser = null; + Iterator formatIter = dateFormats.iterator(); + + while (formatIter.hasNext()) { + String format = (String) formatIter.next(); + if (dateParser == null) { + dateParser = new SimpleDateFormat(format, Locale.US); + dateParser.setTimeZone(TimeZone.getTimeZone("GMT")); + dateParser.set2DigitYearStart(startDate); + } else { + dateParser.applyPattern(format); + } + try { + return dateParser.parse(dateValue); + } catch (ParseException pe) { + // ignore this exception, we will try the next format + } + } + + // we were unable to parse the date + throw new DateParseException("Unable to parse the date " + dateValue); + } + + /** + * Formats the given date according to the RFC 1123 pattern. + * + * @param date The date to format. + * @return An RFC 1123 formatted date string. + * + * @see #PATTERN_RFC1123 + */ + public static String formatDate(Date date) { + return formatDate(date, PATTERN_RFC1123); + } + + /** + * Formats the given date according to the specified pattern. The pattern + * must conform to that used by the {@link SimpleDateFormat simple date + * format} class. + * + * @param date The date to format. + * @param pattern The pattern to use for formatting the date. + * @return A formatted date string. + * + * @throws IllegalArgumentException If the given date pattern is invalid. + * + * @see SimpleDateFormat + */ + public static String formatDate(Date date, String pattern) { + if (date == null) throw new IllegalArgumentException("date is null"); + if (pattern == null) throw new IllegalArgumentException("pattern is null"); + + SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US); + formatter.setTimeZone(GMT); + return formatter.format(date); + } + + /** This class should not be instantiated. */ + private DateUtil() { } + +} Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/EncodingUtil.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/EncodingUtil.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/EncodingUtil.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/EncodingUtil.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,274 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.util; + +import org.slf4j.LoggerFactory; +import org.slf4j.Logger; +import org.apache.commons.codec.net.URLCodec; + +import java.io.UnsupportedEncodingException; + +/** + * The home for utility methods that handle various encoding tasks. + * + * @author Michael Becke + * @author Oleg Kalnichevski + * + * @since 2.0 final + */ +public class EncodingUtil { + + final static Logger LOG = LoggerFactory.getLogger(EncodingUtil.class); + + /** Default content encoding chatset */ + private static final String DEFAULT_CHARSET = "ISO-8859-1"; + + /** + * Form-urlencoding routine. + * + * The default encoding for all forms is `application/x-www-form-urlencoded'. + * A form data set is represented in this media type as follows: + * + * The form field names and values are escaped: space characters are replaced + * by `+', and then reserved characters are escaped as per [URL]; that is, + * non-alphanumeric characters are replaced by `%HH', a percent sign and two + * hexadecimal digits representing the ASCII code of the character. Line breaks, + * as in multi-line text field values, are represented as CR LF pairs, i.e. `%0D%0A'. + * + *

+ * if the given charset is not supported, ISO-8859-1 is used instead. + *

+ * + * @param pairs the values to be encoded + * @param charset the character set of pairs to be encoded + * + * @return the urlencoded pairs + * + * @since 2.0 final + */ + public static String formUrlEncode(NameValuePair[] pairs, String charset) { + try { + return doFormUrlEncode(pairs, charset); + } catch (UnsupportedEncodingException e) { + LOG.error("Encoding not supported: " + charset); + try { + return doFormUrlEncode(pairs, DEFAULT_CHARSET); + } catch (UnsupportedEncodingException fatal) { + // Should never happen. ISO-8859-1 must be supported on all JVMs + throw new AsyncHttpClientException("Encoding not supported: " + + DEFAULT_CHARSET); + } + } + } + + /** + * Form-urlencoding routine. + * + * The default encoding for all forms is `application/x-www-form-urlencoded'. + * A form data set is represented in this media type as follows: + * + * The form field names and values are escaped: space characters are replaced + * by `+', and then reserved characters are escaped as per [URL]; that is, + * non-alphanumeric characters are replaced by `%HH', a percent sign and two + * hexadecimal digits representing the ASCII code of the character. Line breaks, + * as in multi-line text field values, are represented as CR LF pairs, i.e. `%0D%0A'. + * + * @param pairs the values to be encoded + * @param charset the character set of pairs to be encoded + * + * @return the urlencoded pairs + * @throws UnsupportedEncodingException if charset is not supported + * + * @since 2.0 final + */ + private static String doFormUrlEncode(NameValuePair[] pairs, String charset) + throws UnsupportedEncodingException + { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < pairs.length; i++) { + URLCodec codec = new URLCodec(); + NameValuePair pair = pairs[i]; + if (pair.getName() != null) { + if (i > 0) { + buf.append("&"); + } + buf.append(codec.encode(pair.getName(), charset)); + buf.append("="); + if (pair.getValue() != null) { + buf.append(codec.encode(pair.getValue(), charset)); + } + } + } + return buf.toString(); + } + + /** + * 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 offset the index of the first byte to encode + * @param length the number of bytes to encode + * @param charset the desired character encoding + * @return The result of the conversion. + * + * @since 3.0 + */ + public static String getString( + final byte[] data, + int offset, + int length, + String charset + ) { + + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + + if (charset == null || charset.length() == 0) { + throw new IllegalArgumentException("charset may not be null or empty"); + } + + try { + return new String(data, offset, length, charset); + } catch (UnsupportedEncodingException e) { + + if (LOG.isWarnEnabled()) { + LOG.warn("Unsupported encoding: " + charset + ". System encoding used"); + } + return new String(data, offset, length); + } + } + + + /** + * 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 specified string to a byte array. If the charset is not supported the + * default system charset is used. + * + * @param data the string to be encoded + * @param charset the desired character encoding + * @return The resulting byte array. + * + * @since 3.0 + */ + public static byte[] getBytes(final String data, String charset) { + + if (data == null) { + throw new IllegalArgumentException("data may not be null"); + } + + if (charset == null || charset.length() == 0) { + throw new IllegalArgumentException("charset may not be null or empty"); + } + + try { + return data.getBytes(charset); + } catch (UnsupportedEncodingException e) { + + if (LOG.isWarnEnabled()) { + LOG.warn("Unsupported encoding: " + charset + ". System encoding used."); + } + + return data.getBytes(); + } + } + + /** + * Converts the specified string to byte array of ASCII characters. + * + * @param data the string to be encoded + * @return The string as a byte array. + * + * @since 3.0 + */ + public static byte[] getAsciiBytes(final String data) { + + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + + try { + return data.getBytes("US-ASCII"); + } catch (UnsupportedEncodingException e) { + throw new AsyncHttpClientException("HttpClient requires ASCII support"); + } + } + + /** + * 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 + * @param offset the index of the first byte to encode + * @param length the number of bytes to encode + * @return The string representation of the byte array + * + */ + public static String getAsciiString(final byte[] data, int offset, int length) { + + if (data == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + + try { + return new String(data, offset, length, "US-ASCII"); + } catch (UnsupportedEncodingException e) { + throw new AsyncHttpClientException("HttpClient requires ASCII support"); + } + } + + /** + * 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 + * + */ + public static String getAsciiString(final byte[] data) { + return getAsciiString(data, 0, data.length); + } + + /** + * This class should not be instantiated. + */ + private EncodingUtil() { + } + +} + Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/LangUtils.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/LangUtils.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/LangUtils.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/LangUtils.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.util; + +/** + * A set of utility methods to help produce consistent Object#equals(Object) and + * Object#hashCode methods. + * + * @author Oleg Kalnichevski + * + */ +public class LangUtils { + + public static final int HASH_SEED = 17; + public static final int HASH_OFFSET = 37; + + private LangUtils() { + super(); + } + + public static int hashCode(final int seed, final int hashcode) { + return seed * HASH_OFFSET + hashcode; + } + + public static int hashCode(final int seed, final Object obj) { + return hashCode(seed, obj != null ? obj.hashCode() : 0); + } + + public static int hashCode(final int seed, final boolean b) { + return hashCode(seed, b ? 1 : 0); + } + + public static boolean equals(final Object obj1, final Object obj2) { + return obj1 == null ? obj2 == null : obj1.equals(obj2); + } + +} + Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/NameValuePair.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/NameValuePair.java?view=auto&rev=567184 ============================================================================== --- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/NameValuePair.java (added) +++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/util/NameValuePair.java Fri Aug 17 17:03:37 2007 @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.ahc.util; + +import java.io.Serializable; + +public class NameValuePair implements Serializable { + + // ----------------------------------------------------------- Constructors + + /** + * Default constructor. + * + */ + public NameValuePair() { + this (null, null); + } + + /** + * Constructor. + * @param name The name. + * @param value The value. + */ + public NameValuePair(String name, String value) { + this.name = name; + this.value = value; + } + + // ----------------------------------------------------- Instance Variables + + /** + * Name. + */ + private String name = null; + + /** + * Value. + */ + private String value = null; + + // ------------------------------------------------------------- Properties + + /** + * Set the name. + * + * @param name The new name + * @see #getName() + */ + public void setName(String name) { + this.name = name; + } + + + /** + * Return the name. + * + * @return String name The name + * @see #setName(String) + */ + public String getName() { + return name; + } + + + /** + * Set the value. + * + * @param value The new value. + */ + public void setValue(String value) { + this.value = value; + } + + + /** + * Return the current value. + * + * @return String value The current value. + */ + public String getValue() { + return value; + } + + // --------------------------------------------------------- Public Methods + + /** + * Get a String representation of this pair. + * @return A string representation. + */ + public String toString() { + return ("name=" + name + ", " + "value=" + value); + } + + public boolean equals(final Object object) { + if (object == null) return false; + if (this == object) return true; + if (object instanceof NameValuePair) { + NameValuePair that = (NameValuePair) object; + return LangUtils.equals(this.name, that.name) + && LangUtils.equals(this.value, that.value); + } else { + return false; + } + } + + public int hashCode() { + int hash = LangUtils.HASH_SEED; + hash = LangUtils.hashCode(hash, this.name); + hash = LangUtils.hashCode(hash, this.value); + return hash; + } +} + Added: geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/conf/keystore URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/conf/keystore?view=auto&rev=567184 ============================================================================== Binary file - no diff available. Propchange: geronimo/sandbox/AsyncHttpClient/src/test/catalina/var/catalina/conf/keystore ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream