View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic 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   * Vectomatic 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 Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.server;
19  
20  import java.io.BufferedInputStream;
21  import java.io.BufferedOutputStream;
22  import java.io.IOException;
23  import java.util.logging.Logger;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  /**
31   * GAE-based servlet to echo with the proper content-type the
32   * SVG posted by a Vectomatic client
33   */
34  public class ExportServlet extends HttpServlet {
35  	private static final long serialVersionUID = 1L;
36  	private static Logger logger = Logger.getLogger(ExportServlet.class.getName());
37  
38      /**
39       * Default constructor. 
40       */
41      public ExportServlet() {
42      }
43  
44  	/**
45  	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
46  	 */
47  	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48  		logger.info("host:" + request.getRemoteHost() + " address:" + request.getRemoteAddr() + " user:" + request.getRemoteUser());
49  		response.setContentType("text/html");
50  		response.getWriter().println("<html><head></head><body><p>ExportServlet alive</p></body></html>");
51  	}
52  
53  	/**
54  	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
55  	 */
56  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57  		logger.info("host:" + request.getRemoteHost() + " address:" + request.getRemoteAddr() + " user:" + request.getRemoteUser());
58  		response.setContentType("image/svg+xml");
59  		BufferedInputStream input = new BufferedInputStream(request.getInputStream());
60  		BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
61  		char[] paramName = {'d', 'a', 't', 'a','='};
62  		int c, count = 0;
63  		while ((c = input.read()) != -1) {
64  			if (paramName[count] == c) {
65  				count++;
66  				if (count == paramName.length) {
67  					break;
68  				}
69  			} else {
70  				count = 0;
71  			}
72  		}
73  		if (c == -1) {
74  			throw new IOException("No data parameter found");
75  		}
76  		while ((c = input.read()) != -1) {
77  			switch(c) {
78  				case '+':
79  					output.write(' ');
80  					break;
81  				case '%':
82  					int h = fromBase16(input.read());
83  					int l = fromBase16(input.read());
84  					output.write((byte)(h * 16 + l));
85  					break;
86  				default:
87  					output.write((byte)c);
88  			}
89  		}
90  		output.flush();
91  	}
92  	
93  	public int fromBase16(int i) throws IOException {
94  		if (i >= '0' && i <= '9') {
95  			return i - '0';
96  		} else if (i >= 'A' && i <= 'F') {
97  			return 10 + i - 'A';
98  		} else {
99  			throw new IOException("Encoding error: " + i);
100 		}
101 	}
102 }