Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / IppRequestFactory.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.lang.reflect.Constructor;
22 import java.net.URI;
23
24 import javax.print.attribute.AttributeSet;
25 import javax.print.attribute.HashAttributeSet;
26 import javax.print.attribute.standard.RequestingUserName;
27
28 import de.lohndirekt.print.attribute.auth.RequestingUserPassword;
29 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OperationsSupported;
30
31 /**
32 * @author bpusch
33 *
34 */
35 public final class IppRequestFactory {
36
37 final static String IPP_REQUEST_IMPL_KEY = "de.lohndirekt.print.IppRequest.Impl";
38
39 final static IppRequest createIppRequest(URI uri,
40 OperationsSupported operation, RequestingUserName user,
41 RequestingUserPassword passwd) {
42 String requestClassName = System.getProperty(IPP_REQUEST_IMPL_KEY);
43 IppRequest request = null;
44 if (requestClassName == null) {
45 request = new IppRequestCupsImpl(uri, operation);
46 } else {
47 Class clazz = null;
48 try {
49 clazz = Class.forName(requestClassName);
50 } catch (ClassNotFoundException e) {
51 throw new IllegalArgumentException("Class " + requestClassName
52 + " does not exist.");
53 }
54
55 try {
56 Constructor constructor = clazz
57 .getDeclaredConstructor(new Class[] { URI.class,
58 OperationsSupported.class });
59 request = (IppRequest) constructor.newInstance(new Object[] {
60 uri, operation });
61 } catch (Exception e) {
62 throw new RuntimeException(e);
63 }
64 }
65 if (user != null && passwd != null) {
66 AttributeSet set = new HashAttributeSet();
67 set.add(user);
68 set.add(passwd);
69 request.addOperationAttributes(set);
70 }
71 return request;
72 }
73 }