View Javadoc

1   package org.molwind.servlet;
2   /*
3    * This file is part of Molwind.
4    *
5    * Molwind is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * Molwind is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  
20  import org.molwind.util.MolwindServerConfiguration;
21  import org.molwind.util.MolwindLogger;
22  import org.molwind.model.LayeredPosition;
23  import org.molwind.view.Tiles;
24  
25  
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.OutputStream;
30  
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.ServletOutputStream;
35  
36  
37  
38  
39  
40  /**
41   *  GetWorldNamesCommand implements a Command which can be invoked by a client via Webservice 
42   * to get Images
43   * 
44   *
45   * @author <a href="mailto:Oktay.Degirmenci@merck.de">Oktay Degirmenci</a>
46   */
47  public class GetImageCommand extends AbstractCommand{
48      
49  
50      ServletOutputStream out = null;
51     
52      private String DEFAULT_IMGLOCATION="/resources";//TODO find a good default_imagelocation
53      private String imageLocation=DEFAULT_IMGLOCATION;
54      File image = null;
55     
56         /**
57       * Name of the parameter, which includes the name of the world.
58       */
59      public static final String IMAGE_PARAM = "image";
60     
61      public static final String PARAM_WORLD = "world";
62  
63      /**
64       * Default name of a Molwind world.
65       */
66      public static final String DEFAULT_WORLD = "default";
67      
68      /**
69       * Name of the parameter, which includes the name of the called command
70       */
71      public static final String PARAM_COMMAND = "command";
72      
73      /**
74       * Default name of a command
75       */
76      public static final String DEFAULT_COMMAND ="defCommand";
77  
78  
79      /**
80       * Get the ImageLocation value.
81       * @return the ImageLocation value.
82       */
83      public String getImageLocation() {
84  	return imageLocation;
85      }
86  
87      /**
88       * Set the ImageLocation value.
89       * @param newImageLocation The new ImageLocation value.
90       */
91      public void setImageLocation(String newImageLocation) {
92  	this.imageLocation = newImageLocation;
93      }
94  
95      
96        /**
97       * Reads the requested position from the request object.
98       *
99       * @param request
100      *      an {@link javax.servlet.http.HttpServletRequest} object, contains
101      *      the request the client has made of the servlet
102      * @return
103      *      the parsed position
104      */
105     private LayeredPosition parsePosition(final HttpServletRequest request) {
106         int x = parseInt(request, "X", 0);
107         int y = parseInt(request, "Y", 0);
108         int l = parseInt(request, "L", 0);
109         return new LayeredPosition(x, y, l);
110     }
111 
112     /**
113      * Tries to read the world name from the request object.
114      *
115      * @param request
116      *      an {@link javax.servlet.http.HttpServletRequest} object, contains
117      *      the request the client has made of the servlet
118      * @return
119      *      the parsed world name
120      */
121     private String parseWorldName(final HttpServletRequest request) {
122         String worldName = request.getParameter(PARAM_WORLD);
123 
124         if (worldName == null) {
125             return DEFAULT_WORLD;
126         } else {
127             return worldName;
128         }
129     }
130 
131     
132     /**
133      * Tries to read integer values from the request object.
134      *
135      * @param request
136      *      an {@link javax.servlet.http.HttpServletRequest} object, contains
137      *      the request the client has made of the servlet
138      * @param paramName
139      *      the name of the parameter
140      * @param defaultValue
141      *      the default value
142      * @return
143      *      the parsed integer
144      */
145     private int parseInt(final HttpServletRequest request,
146             final String paramName, final int defaultValue) {
147         int x = defaultValue;
148         String paramString = request.getParameter(paramName);
149 
150         if (paramString != null) {
151             try {
152                 x = Integer.parseInt(paramString);
153             } catch (NumberFormatException nfe) {
154                 MolwindLogger.warn("Request parameter "
155                         + paramName + " is invalid");
156                 x = defaultValue;
157             }
158         }
159 
160         return x;
161     }
162 
163 
164     /**
165      * Executes the GetImageCommand
166      * @param request
167      *      an {@link javax.servlet.http.HttpServletRequest} object, contains
168      *      the request the client has made of the servlet
169      * 
170      * @param response 
171      *      an {@link javax.servlet.http.HttpServletResponse} object, contains
172      *      the response the client will get from the servlet
173      *      
174      * 
175      */
176     public void  execute(HttpServletRequest request,HttpServletResponse response){
177 
178 	System.out.println("Es lebt");
179     
180     }
181 
182 
183       
184      /**
185      * Executes the GetImageCommand
186      * @param request
187      *      an {@link javax.servlet.http.HttpServletRequest} object, contains
188      *      the request the client has made of the servlet
189      * 
190      * @param response 
191      *      an {@link javax.servlet.http.HttpServletResponse} object, contains
192      *      the response the client will get from the servlet
193      *      
194      * 
195      */
196    //  public void  execute(HttpServletRequest request,HttpServletResponse response)
197 //     {
198 //         MolwindLogger.debug("Parsing request: " + request);
199 //         String worldName = parseWorldName(request);
200 
201 //         LayeredPosition position = parsePosition(request);
202 //         MolwindLogger.debug("World: "
203 //                 + worldName + ", position request: " + position);
204 
205 
206 //         if (position.isOrigin()) {
207 // 	    System.err.println("Is Origin!!!");
208 //             // TODO Dispatch to some other action
209 	   
210 //         } else {
211 // 	    try{
212 // 		Tiles tiles = Tiles.getInstance(worldName);
213 // 		response.setContentType("image/jpg");
214 // 		MolwindLogger.debug("Writing tile of " + position);
215 // 		OutputStream out = response.getOutputStream();
216 	
217 	
218 // 	      	tiles.writeTile(position,out);
219 // 	    }catch(IOException ioe){
220 
221 // 		ioe.printStackTrace();
222 
223 // 	    }
224 
225 	   
226 //         }
227 
228 	
229 
230 //     }
231 
232 }