View Javadoc

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