| 1 | /**\r |
| 2 | * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r |
| 3 | *\r |
| 4 | * This library is free software; you can redistribute it and/or\r |
| 5 | * modify it under the terms of the GNU Lesser General Public\r |
| 6 | * License as published by the Free Software Foundation; either\r |
| 7 | * version 2.1 of the License, or (at your option) any later version.\r |
| 8 | * \r |
| 9 | * This library is distributed in the hope that it will be useful,\r |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r |
| 12 | * Lesser General Public License for more details.\r |
| 13 | * \r |
| 14 | * You should have received a copy of the GNU Lesser General Public\r |
| 15 | * License along with this library; if not, write to the Free Software\r |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
| 17 | * \r |
| 18 | */\r |
| 19 | package de.lohndirekt.print;\r |
| 20 | \r |
| 21 | import java.io.IOException;\r |
| 22 | import java.io.InputStream;\r |
| 23 | import java.net.HttpURLConnection;\r |
| 24 | import java.net.URI;\r |
| 25 | import java.net.URISyntaxException;\r |
| 26 | import java.util.logging.Level;\r |
| 27 | import java.util.logging.Logger;\r |
| 28 | \r |
| 29 | import org.apache.commons.httpclient.Credentials;\r |
| 30 | import org.apache.commons.httpclient.HttpClient;\r |
| 31 | import org.apache.commons.httpclient.HttpException;\r |
| 32 | import org.apache.commons.httpclient.UsernamePasswordCredentials;\r |
| 33 | import org.apache.commons.httpclient.methods.EntityEnclosingMethod;\r |
| 34 | import org.apache.commons.httpclient.methods.PostMethod;\r |
| 35 | \r |
| 36 | import de.lohndirekt.print.exception.AuthenticationException;\r |
| 37 | \r |
| 38 | \r |
| 39 | /**\r |
| 40 | * simple facade / abstraction layer to "commons httpclient"\r |
| 41 | * \r |
| 42 | * @author sefftinge\r |
| 43 | * \r |
| 44 | */\r |
| 45 | class IppHttpConnection implements IppConnection {\r |
| 46 | \r |
| 47 | private Logger log = Logger.getLogger(this.getClass().getName());\r |
| 48 | \r |
| 49 | private HttpClient httpConn;\r |
| 50 | \r |
| 51 | private PostMethod method;\r |
| 52 | \r |
| 53 | /**\r |
| 54 | * @param uri\r |
| 55 | * @param user\r |
| 56 | * @param passwd\r |
| 57 | * @param useStream\r |
| 58 | */\r |
| 59 | public IppHttpConnection(URI uri, String user, String passwd) {\r |
| 60 | try {\r |
| 61 | httpConn = new HttpClient();\r |
| 62 | method = new PostMethod(toHttpURI(uri).toString());\r |
| 63 | method.addRequestHeader("Content-type", "application/ipp");\r |
| 64 | method.addRequestHeader("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");\r |
| 65 | method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_AUTO);\r |
| 66 | // authentication\r |
| 67 | if (user != null && user.trim().length() > 0) {\r |
| 68 | if (log.isLoggable(Level.FINER)) {\r |
| 69 | log.log(Level.SEVERE, "Using username: "+user+" , passwd.length "+passwd.length());\r |
| 70 | }\r |
| 71 | method.setDoAuthentication(true);\r |
| 72 | Credentials creds = new UsernamePasswordCredentials(user,\r |
| 73 | passwd);\r |
| 74 | httpConn.getState().setCredentials(null,\r |
| 75 | toHttpURI(uri).getHost(), creds);\r |
| 76 | \r |
| 77 | }\r |
| 78 | \r |
| 79 | } catch (Exception e) {\r |
| 80 | log.log(Level.SEVERE, e.getMessage(), e);\r |
| 81 | }\r |
| 82 | }\r |
| 83 | \r |
| 84 | \r |
| 85 | /**\r |
| 86 | * @return content of the response\r |
| 87 | * @throws IOException\r |
| 88 | */\r |
| 89 | public InputStream getIppResponse() throws IOException {\r |
| 90 | return method.getResponseBodyAsStream();\r |
| 91 | }\r |
| 92 | \r |
| 93 | /**\r |
| 94 | * @return the statuscode of last request\r |
| 95 | * @throws IOException\r |
| 96 | */\r |
| 97 | public int getStatusCode() throws IOException {\r |
| 98 | return method.getStatusCode();\r |
| 99 | }\r |
| 100 | \r |
| 101 | private static URI toHttpURI(URI uri) {\r |
| 102 | if (uri.getScheme().equals("ipp")) {\r |
| 103 | String uriString = uri.toString().replaceFirst("ipp", "http");\r |
| 104 | // TODO remove this hack!\r |
| 105 | // uriString = uriString.replaceAll("(\\d{1,3}\\.){3}\\d{1,3}:\\d{1,}", "127.0.0.1:631");\r |
| 106 | // endof hack\r |
| 107 | try {\r |
| 108 | uri = new URI(uriString);\r |
| 109 | } catch (URISyntaxException e) {\r |
| 110 | throw new RuntimeException("toHttpURI buggy? : uri was " + uri);\r |
| 111 | }\r |
| 112 | }\r |
| 113 | return uri;\r |
| 114 | }\r |
| 115 | \r |
| 116 | /**\r |
| 117 | * @param stream\r |
| 118 | */\r |
| 119 | public void setIppRequest(InputStream stream) {\r |
| 120 | method.setRequestBody(stream);\r |
| 121 | }\r |
| 122 | \r |
| 123 | public boolean execute() throws HttpException, IOException {\r |
| 124 | if (this.method.validate()) {\r |
| 125 | httpConn.executeMethod(method);\r |
| 126 | if (this.getStatusCode()==HttpURLConnection.HTTP_UNAUTHORIZED) {\r |
| 127 | throw new AuthenticationException(method.getStatusText());\r |
| 128 | }\r |
| 129 | return true;\r |
| 130 | } else {\r |
| 131 | return false;\r |
| 132 | }\r |
| 133 | }\r |
| 134 | \r |
| 135 | } |