2 * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package de
.lohndirekt
.print
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.net
.HttpURLConnection
;
25 import java
.net
.URISyntaxException
;
26 import java
.util
.logging
.Level
;
27 import java
.util
.logging
.Logger
;
29 import org
.apache
.commons
.httpclient
.Credentials
;
30 import org
.apache
.commons
.httpclient
.HttpClient
;
31 import org
.apache
.commons
.httpclient
.HttpException
;
32 import org
.apache
.commons
.httpclient
.UsernamePasswordCredentials
;
33 import org
.apache
.commons
.httpclient
.methods
.EntityEnclosingMethod
;
34 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
36 import de
.lohndirekt
.print
.exception
.AuthenticationException
;
40 * simple facade / abstraction layer to "commons httpclient"
45 class IppHttpConnection
implements IppConnection
{
47 private Logger log
= Logger
.getLogger(this.getClass().getName());
49 private HttpClient httpConn
;
51 private PostMethod method
;
59 public IppHttpConnection(URI uri
, String user
, String passwd
) {
61 httpConn
= new HttpClient();
62 method
= new PostMethod(toHttpURI(uri
).toString());
63 method
.addRequestHeader("Content-type", "application/ipp");
64 method
.addRequestHeader("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
65 method
.setRequestContentLength(EntityEnclosingMethod
.CONTENT_LENGTH_AUTO
);
67 if (user
!= null && user
.trim().length() > 0) {
68 if (log
.isLoggable(Level
.FINER
)) {
69 log
.log(Level
.SEVERE
, "Using username: "+user
+" , passwd.length "+passwd
.length());
71 method
.setDoAuthentication(true);
72 Credentials creds
= new UsernamePasswordCredentials(user
,
74 httpConn
.getState().setCredentials(null,
75 toHttpURI(uri
).getHost(), creds
);
79 } catch (Exception e
) {
80 log
.log(Level
.SEVERE
, e
.getMessage(), e
);
86 * @return content of the response
89 public InputStream
getIppResponse() throws IOException
{
90 return method
.getResponseBodyAsStream();
94 * @return the statuscode of last request
97 public int getStatusCode() throws IOException
{
98 return method
.getStatusCode();
101 private static URI
toHttpURI(URI uri
) {
102 if (uri
.getScheme().equals("ipp")) {
103 String uriString
= uri
.toString().replaceFirst("ipp", "http");
104 // TODO remove this hack!
105 // uriString = uriString.replaceAll("(\\d{1,3}\\.){3}\\d{1,3}:\\d{1,}", "127.0.0.1:631");
108 uri
= new URI(uriString
);
109 } catch (URISyntaxException e
) {
110 throw new RuntimeException("toHttpURI buggy? : uri was " + uri
);
119 public void setIppRequest(InputStream stream
) {
120 method
.setRequestBody(stream
);
123 public boolean execute() throws HttpException
, IOException
{
124 if (this.method
.validate()) {
125 httpConn
.executeMethod(method
);
126 if (this.getStatusCode()==HttpURLConnection
.HTTP_UNAUTHORIZED
) {
127 throw new AuthenticationException(method
.getStatusText());