Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: Serhan AKIN $'
7
 *     '$Date: 2009-06-13 13:28:21 +0300  $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
package edu.ucsb.nceas.metacat.restservice;
24

    
25
import java.io.IOException;
26

    
27
import javax.servlet.ServletConfig;
28
import javax.servlet.ServletException;
29
import javax.servlet.http.HttpServlet;
30
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpServletResponse;
32

    
33
import org.apache.log4j.Logger;
34

    
35
/**
36
 * Metacat implemantation of Earthgrid (Ecogrid) REST API as a servlet. In each request
37
 * REST Servlet initialize a D1ResourceHandler object and then D1ResourceHandler object 
38
 * handles with request and writes approriate response. 
39
 *  
40
 */
41
public class D1RestServlet extends HttpServlet {
42

    
43
    protected Logger logMetacat;
44
    protected D1ResourceHandler handler;
45

    
46
    /**
47
     * Subclasses should override this method to provide the appropriate handler subclass
48
     * @param request
49
     * @param response
50
     * @return
51
     * @throws ServletException
52
     * @throws IOException
53
     */
54
    protected D1ResourceHandler createHandler(HttpServletRequest request, HttpServletResponse response) 
55
		throws ServletException, IOException {
56
	    D1ResourceHandler handler = new D1ResourceHandler(getServletContext(), request, response);
57
	    return handler;
58
	}
59
    
60
    /**
61
     * Initalize servlet by setting logger
62
     */
63
    @Override
64
    public void init(ServletConfig config) throws ServletException {
65
        logMetacat = Logger.getLogger(this.getClass());
66
        super.init(config);
67
    }
68

    
69
    /** Handle "GET" method requests from HTTP clients */
70
    @Override
71
    protected void doGet(HttpServletRequest request,
72
            HttpServletResponse response) throws ServletException, IOException {
73
        System.out.println("HTTP Verb: GET");
74
        handler = createHandler(request, response);
75
        handler.handle(D1ResourceHandler.GET);
76
    }
77

    
78
    /** Handle "POST" method requests from HTTP clients */
79
    @Override
80
    protected void doPost(HttpServletRequest request,
81
            HttpServletResponse response) throws ServletException, IOException {
82
        System.out.println("HTTP Verb: POST");
83
        handler = createHandler(request, response);
84
        handler.handle(D1ResourceHandler.POST);
85
    }
86

    
87
    /** Handle "DELETE" method requests from HTTP clients */
88
    @Override
89
    protected void doDelete(HttpServletRequest request,
90
            HttpServletResponse response) throws ServletException, IOException {
91
        System.out.println("HTTP Verb: DELETE");
92
        handler = createHandler(request, response);
93
        handler.handle(D1ResourceHandler.DELETE);
94
    }
95

    
96
    /** Handle "PUT" method requests from HTTP clients */
97
    @Override
98
    protected void doPut(HttpServletRequest request,
99
            HttpServletResponse response) throws ServletException, IOException {
100
        System.out.println("HTTP Verb: PUT");
101
        handler = createHandler(request, response);
102
        handler.handle(D1ResourceHandler.PUT);
103
    }
104

    
105
    /** Handle "PUT" method requests from HTTP clients */
106
    @Override
107
    protected void doHead(HttpServletRequest request,
108
            HttpServletResponse response) throws ServletException, IOException {
109
        System.out.println("HTTP Verb: HEAD");
110
        handler = createHandler(request, response);
111
        handler.handle(D1ResourceHandler.HEAD);
112
    }
113
}
(5-5/9)