Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / IppHttpConnection.java
1 /**
2 * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19 package de.lohndirekt.print;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.HttpURLConnection;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
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;
35
36 import de.lohndirekt.print.exception.AuthenticationException;
37
38
39 /**
40 * simple facade / abstraction layer to "commons httpclient"
41 *
42 * @author sefftinge
43 *
44 */
45 class IppHttpConnection implements IppConnection {
46
47 private Logger log = Logger.getLogger(this.getClass().getName());
48
49 private HttpClient httpConn;
50
51 private PostMethod method;
52
53 /**
54 * @param uri
55 * @param user
56 * @param passwd
57 * @param useStream
58 */
59 public IppHttpConnection(URI uri, String user, String passwd) {
60 try {
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);
66 // authentication
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());
70 }
71 method.setDoAuthentication(true);
72 Credentials creds = new UsernamePasswordCredentials(user,
73 passwd);
74 httpConn.getState().setCredentials(null,
75 toHttpURI(uri).getHost(), creds);
76
77 }
78
79 } catch (Exception e) {
80 log.log(Level.SEVERE, e.getMessage(), e);
81 }
82 }
83
84
85 /**
86 * @return content of the response
87 * @throws IOException
88 */
89 public InputStream getIppResponse() throws IOException {
90 return method.getResponseBodyAsStream();
91 }
92
93 /**
94 * @return the statuscode of last request
95 * @throws IOException
96 */
97 public int getStatusCode() throws IOException {
98 return method.getStatusCode();
99 }
100
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");
106 // endof hack
107 try {
108 uri = new URI(uriString);
109 } catch (URISyntaxException e) {
110 throw new RuntimeException("toHttpURI buggy? : uri was " + uri);
111 }
112 }
113 return uri;
114 }
115
116 /**
117 * @param stream
118 */
119 public void setIppRequest(InputStream stream) {
120 method.setRequestBody(stream);
121 }
122
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());
128 }
129 return true;
130 } else {
131 return false;
132 }
133 }
134
135 }