1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.portals.bridges.mapserver;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.PrintWriter;
23 import java.util.Map;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletRequestWrapper;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpServletResponseWrapper;
29
30 import javax.portlet.PortletConfig;
31 import javax.portlet.PortletException;
32 import javax.portlet.RenderRequest;
33 import javax.portlet.RenderResponse;
34 import javax.portlet.ActionRequest;
35 import javax.portlet.ActionResponse;
36 import javax.portlet.GenericPortlet;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /***
42 * This portlet is executes the mapserv binary and encapsulating the query
43 *parameters passed to it.
44 *
45 * @author <a href="mailto:philip.donaghy@gmail.com">Philip Mark Donaghy</a>
46 */
47
48 public class MapServerPortlet extends GenericPortlet {
49
50 private static final Log log = LogFactory.getLog(MapServerPortlet.class);
51
52 private static String MAPSERV_BINARY = "MapServBinary";
53
54 private static String LAYERS = "Layers";
55
56 private static String ZOOM_DIRECTION = "ZoomDirection";
57
58 private static String ZOOM_SIZE = "ZoomSize";
59
60 private static String MAP_FILE = "MapFile";
61
62 private static String PROGRAM = "Program";
63
64 private static String ROOT_URL = "RootURL";
65
66 private static String MAP_WEB_IMAGE_PATH = "MapWebImagePath";
67
68 private static String MAP_WEB_IMAGE_URL = "MapWebImageURL";
69
70 private static String MAP_WEB_TEMPLATE = "MapWebTemplate";
71
72 private String mapservBinary;
73
74 private String layers;
75
76 private String zoomDirection;
77
78 private String zoomSize;
79
80 private String mapFile;
81
82 private String program;
83
84 private String rootURL;
85
86 private String mapWebImagePath;
87
88 private String mapWebImageURL;
89
90 private String mapWebTemplate;
91
92 public void init(PortletConfig config) throws PortletException
93 {
94
95 super.init(config);
96
97
98 this.mapservBinary = config.getInitParameter(MAPSERV_BINARY);
99 this.layers = config.getInitParameter(LAYERS);
100 this.zoomDirection = config.getInitParameter(ZOOM_DIRECTION);
101 this.zoomSize = config.getInitParameter(ZOOM_SIZE);
102 this.mapFile = config.getInitParameter(MAP_FILE);
103 this.program = config.getInitParameter(PROGRAM);
104 this.rootURL = config.getInitParameter(ROOT_URL);
105 this.mapWebImagePath = config.getInitParameter(MAP_WEB_IMAGE_PATH);
106 this.mapWebImageURL = config.getInitParameter(MAP_WEB_IMAGE_URL);
107 this.mapWebTemplate = config.getInitParameter(MAP_WEB_TEMPLATE);
108
109
110 if (mapservBinary == null)
111 {
112 throw new PortletException("Portlet " + config.getPortletName()
113 + " is incorrectly configured. Init parameter "
114 + MAPSERV_BINARY + " not specified");
115 }
116 if (layers == null)
117 {
118 throw new PortletException("Portlet " + config.getPortletName()
119 + " is incorrectly configured. Init parameter "
120 + LAYERS + " not specified");
121 }
122 if (zoomDirection == null)
123 {
124 throw new PortletException("Portlet " + config.getPortletName()
125 + " is incorrectly configured. Init parameter "
126 + ZOOM_DIRECTION + " not specified");
127 }
128 if (zoomSize == null)
129 {
130 throw new PortletException("Portlet " + config.getPortletName()
131 + " is incorrectly configured. Init parameter "
132 + ZOOM_SIZE + " not specified");
133 }
134 if (mapFile == null)
135 {
136 throw new PortletException("Portlet " + config.getPortletName()
137 + " is incorrectly configured. Init parameter "
138 + MAP_FILE + " not specified");
139 }
140 if (program == null)
141 {
142 throw new PortletException("Portlet " + config.getPortletName()
143 + " is incorrectly configured. Init parameter "
144 + PROGRAM + " not specified");
145 }
146 if (rootURL == null)
147 {
148 throw new PortletException("Portlet " + config.getPortletName()
149 + " is incorrectly configured. Init parameter "
150 + ROOT_URL + " not specified");
151 }
152 if (mapWebImagePath == null)
153 {
154 throw new PortletException("Portlet " + config.getPortletName()
155 + " is incorrectly configured. Init parameter "
156 + MAP_WEB_IMAGE_PATH + " not specified");
157 }
158 if (mapWebImageURL == null)
159 {
160 throw new PortletException("Portlet " + config.getPortletName()
161 + " is incorrectly configured. Init parameter "
162 + MAP_WEB_IMAGE_URL + " not specified");
163 }
164 if (mapWebTemplate == null)
165 {
166 throw new PortletException("Portlet " + config.getPortletName()
167 + " is incorrectly configured. Init parameter "
168 + MAP_WEB_TEMPLATE + " not specified");
169 }
170
171 }
172
173 /***
174 * processAction()
175 * Process actions made to the MapServer
176 * @param actionRequest
177 * @param actionResponse
178 * @throws PortletException
179 * @throws IOException
180 */
181 public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
182 throws PortletException, IOException
183 {
184
185 Map parameterMap = actionRequest.getParameterMap();
186 String queryString = ((HttpServletRequest)
187 ((HttpServletRequestWrapper) actionRequest)
188 .getRequest()).getQueryString();
189 }
190
191 /***
192 * doView
193 */
194 public void doView(RenderRequest request, RenderResponse response)
195 throws PortletException, IOException
196 {
197
198
199 response.setContentType("text/html");
200
201
202
203 String command = this.mapservBinary
204 + " QUERY_STRING='layer=" + this.layers
205 + "&zoomdir=" + this.zoomDirection
206 + "&zoomsize=" + this.zoomSize
207 + "&map=" + this.mapFile
208 + "&program=" + this.program
209 + "&root=" + this.rootURL
210 + "&map_web_imagepath=" + this.mapWebImagePath
211 + "&map_web_imageurl=" + this.mapWebImageURL
212 + "&map_web_template=" + this.mapWebTemplate + "'";
213
214
215 String queryString = ((HttpServletRequest)
216 ((HttpServletRequestWrapper) request)
217 .getRequest()).getQueryString();
218 System.out.println("QUERY_STRING : " + queryString);
219 if (queryString != null)
220 {
221 command = this.mapservBinary
222 + " QUERY_STRING=" + queryString;
223 }
224
225 System.out.println("COMMAND : " + command);
226 Process proc = Runtime.getRuntime().exec(command);
227
228
229 InputStream in = proc.getInputStream();
230 InputStreamReader isr = new InputStreamReader(in, "UTF-8");
231 BufferedReader perlResult = new BufferedReader(isr);
232 StringBuffer page = new StringBuffer();
233
234
235 boolean bProcDone = false;
236 while (bProcDone == false)
237 {
238 try
239 {
240 proc.exitValue() ;
241 bProcDone = true;
242 }
243 catch(IllegalThreadStateException e)
244 {
245 bProcDone = false;
246
247
248
249 int ln;
250 while ((ln = perlResult.read()) != -1)
251 {
252 char c = (char)ln;
253 if (c != '\n' && c != '\r')
254 page.append((char)ln);
255 }
256 }
257 }
258
259
260 int ln = -1;
261
262 while ((ln = perlResult.read()) != -1)
263 {
264 char c = (char)ln;
265 if (c != '\n' && c != '\r')
266 page.append((char)ln);
267 }
268
269 perlResult.close();
270
271
272 HttpServletResponse httpResponse = (HttpServletResponse)
273 ((HttpServletResponseWrapper) response).getResponse();
274
275 PrintWriter writer = httpResponse.getWriter();
276 writer.println(page.toString());
277 writer.flush();
278 writer.close();
279 }
280
281 }