summaryrefslogtreecommitdiffstats
path: root/das-java/tags/1.0-incubating-beta2-rc1/samples/advanced-webapp/src/main/java/org/apache/tuscany/samples/web/CommandServlet.java
blob: 26a1f468a7716933da63b528c3d7da55f444ac45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.samples.web;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tuscany.das.rdb.dbconfig.DBInitializer;
/**
 * This is generic servlet where based on serviceName, it will call the speciliazed processor to
 * process request and obtain response. This servlet can be used in any web sample, just needs to 
 * pass appropriate serviceProcessorClassName and rest is generic 
 *
 */
public class CommandServlet extends HttpServlet {
    final static long serialVersionUID = 1221332432;
	/*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public CommandServlet() {
        super();
    }
    
    public static void refreshDB() throws ServletException {
    	try{
    		DBInitializer dbInitializer = new DBInitializer("AjaxDBConfig.xml");
	    	dbInitializer.refreshDatabaseData();
		}catch(Exception e){
			e.printStackTrace();
			ServletException se = new ServletException(e.getMessage());
			throw se;
		}
    }

    public void init(ServletConfig config) throws ServletException { 
    	refreshDB();
    }
    
    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0,
     *      HttpServletResponse arg1)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doTask(request, response);
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0,
     *      HttpServletResponse arg1)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doTask(request, response);
    }

    /**Invoke passed service and return result
     * 
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
	public void doTask(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		String textContentType= "text/text";
		String xmlContentType= "text/xml";
		
		String refreshRequest = req.getParameter("refreshDB"); 
		if(refreshRequest != null && refreshRequest.equals("yes")){
			{
				try{
					refreshDB();
				}catch(ServletException e){
					throw e;
				}
			}
			this.writeResponse(resp, "Refreshed database!", textContentType);			
		}
		else{
			String serviceName = req.getParameter("serviceName");//e.g. DASQueryProcessor
			String qry = req.getParameter("Query");//e.g. query:xxx, command:yyy
			String configFile = req.getParameter("configFile");//name of DAS config file
			
			try{
				ServiceProcessor serviceClassInst = null;
				//so any subclass of abstract ServiceProcessor can be used
				Object obj = Class.forName(serviceName).newInstance();
				
				if(obj instanceof ServiceProcessor){
					serviceClassInst = (ServiceProcessor)obj;
				}
				
				String result = serviceClassInst.execute(qry, configFile);
				this.writeResponse(resp, result, xmlContentType);
			}catch(Exception e){
				this.writeResponse(resp, e.getMessage(), textContentType);
				e.printStackTrace();
			}			
		}
	}

	/**Write response in required content type to servlet response
	 * 
	 * @param resp
	 * @param output
	 * @param contentType
	 * @throws IOException
	 */
	public void writeResponse(HttpServletResponse resp, String output, String contentType) throws IOException {
		resp.setContentType(contentType);//"text/text", "text/xml"
		resp.setHeader("Cache-Control", "no-cache");
		resp.getWriter().write(output);		
	}
	
    // Utility methods

    public void gotoURL(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String url) throws ServletException, IOException {
        url = response.encodeURL(url);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }

    public void redirectURL(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String url) throws ServletException, IOException {
        url = response.encodeURL(request.getContextPath() + url);
        response.sendRedirect(url);
    }

    public void forwardURL(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String url) throws ServletException, IOException {
        url = response.encodeURL(url);
        RequestDispatcher dispatch = getServletContext().getRequestDispatcher(url);
        dispatch.forward(request, response);
    }    
}