comparison pdf/src/main/java/digilib/servlet/PDFRequest.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents
children
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
1 package digilib.servlet;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.apache.log4j.Logger;
9
10 import digilib.image.ImageJobDescription;
11 import digilib.io.DocuDirectory;
12 import digilib.io.FileOpException;
13 import digilib.io.FileOps.FileClass;
14 import digilib.util.NumRange;
15 import digilib.util.OptionsSet;
16 import digilib.util.ParameterMap;
17
18
19 /**
20 * A container class for storing a set of instruction parameters
21 * used for content generator classes like MakePDF.
22 *
23 *
24 * @author cmielack, casties
25 *
26 */
27 public class PDFRequest extends ParameterMap {
28
29 DigilibConfiguration dlConfig = null;
30 NumRange pages = null;
31 /** general logger for this class */
32 protected static Logger logger = Logger.getLogger("digilib.servlet");
33
34
35 /**
36 * Initialize the PDFJobInformation
37 *
38 * @param dlcfg
39 * The DigilibConfiguration.
40 */
41 public PDFRequest(DigilibConfiguration dlcfg) {
42 super(30);
43 dlConfig = dlcfg;
44 }
45
46 /**
47 * Initialize the PDFJobInformation with a request.
48 *
49 * @param dlcfg The DigilibConfiguration.
50 * @param request
51 * @throws FileOpException
52 */
53 public PDFRequest(HttpServletRequest request, DigilibConfiguration dlcfg) throws FileOpException {
54 super(30);
55 dlConfig = dlcfg;
56 this.setWithRequest(request);
57 }
58
59
60 protected void initParams() {
61 // page numbers
62 newParameter("pgs", "", null, 's');
63 // url of the page/document (second part)
64 newParameter("fn", "", null, 's');
65 // width of client in pixels
66 newParameter("dw", new Integer(0), null, 's');
67 // height of client in pixels
68 newParameter("dh", new Integer(500), null, 's');
69 }
70
71 /* (non-Javadoc)
72 * @see digilib.servlet.ParameterMap#initOptions()
73 */
74 @Override
75 protected void initOptions() {
76 options = (OptionsSet) getValue("mo");
77 }
78
79 /**
80 * Read the request object.
81 *
82 * @param request
83 * @throws FileOpException
84 */
85 public void setWithRequest(HttpServletRequest request) throws FileOpException {
86 // read matching request parameters for the parameters in this map
87 for (String k : params.keySet()) {
88 if (request.getParameterMap().containsKey(k)) {
89 setValueFromString(k, request.getParameter(k));
90 }
91 }
92 // process parameters
93 pages = new NumRange(getAsString("pgs"));
94 ImageJobDescription ij = ImageJobDescription.getInstance(this, dlConfig);
95 DocuDirectory dir = ij.getFileDirectory();
96 int dirsize = dir.size(FileClass.IMAGE);
97 pages.setMaxnum(dirsize);
98 }
99
100
101 /**
102 * Generate a filename for the pdf to be created.
103 *
104 * @return
105 */
106 public String getDocumentId(){
107 String fn = getAsString("fn");
108 String dh = getAsString("dh");
109 String dw = getAsString("dw");
110 String pgs = getAsString("pgs");
111
112 String id = "fn=" + fn + "&dw=" + dw + "&dh=" + dh + "&pgs=" + pgs + ".pdf";
113 // make safe to use as filename by urlencoding
114 try {
115 id = URLEncoder.encode(id, "UTF-8");
116 } catch (UnsupportedEncodingException e) {
117 // this shouldn't happen
118 }
119 return id;
120 }
121
122
123 public ImageJobDescription getImageJobInformation(){
124 return ImageJobDescription.getInstance(this, dlConfig);
125 }
126
127
128 public NumRange getPages() {
129 return pages;
130 }
131
132
133 /**
134 * Check parameters for validity.
135 * Returns true if no errors are found.
136 *
137 * @return
138 */
139 public boolean checkValidity(){
140 if (pages != null) {
141 return true;
142 }
143 return false;
144 }
145
146 public DigilibConfiguration getDlConfig(){
147 return dlConfig;
148 }
149
150 }