summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcst/server/servlet/wcstServlet.java
blob: 17850a03cd748d8aef89b0fbf581d575088641ab (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
161
162
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
 rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcst.server.servlet;

//~--- non-JDK imports --------------------------------------------------------
import petascope.wcst.server.*;
import petascope.ConfigManager;
import org.apache.commons.io.FileUtils;

//~--- JDK imports ------------------------------------------------------------

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import petascope.wcps.server.core.DbMetadataSource;
import petascope.wcs.server.exceptions.WCSException;

/** Servlet interface for the WCS-Transactional server.
 *
 * @author Andrei Aiordachioaie
 */
public class wcstServlet extends HttpServlet {

    private String defaultHtmlResponse;
    private String relativeServletHtmlPath = "/templates/wcst-servlet.html";
    private String servletHtmlPath;
    private String relativeSettingsPath = "/settings.properties";
    private WcstServer server;
    private DbMetadataSource metadataSource;

    @Override
    public void init() throws ServletException {
        // Initialize the configuration manager. Now all classes can read the settings.
        String settingsPath = getServletContext().getRealPath(relativeSettingsPath);
        ConfigManager config = ConfigManager.getInstance(settingsPath, getServletContext().getRealPath("/"));

        try {
            metadataSource = new DbMetadataSource(
                    ConfigManager.METADATA_DRIVER, ConfigManager.METADATA_URL,
                    ConfigManager.METADATA_USER, ConfigManager.METADATA_PASS, false);
        } catch (Exception e) {
            throw new ServletException("Metadata Database Error.", e);
        }

        // Initialize the WCS-T server with proper metadata
        try {
            server = new WcstServer(metadataSource);
        } catch (WCSException e) {
            throw new ServletException(e);
        }

        // Load the servlet HTML response
        servletHtmlPath = getServletContext().getRealPath(relativeServletHtmlPath);
        try {
            defaultHtmlResponse = FileUtils.readFileToString(new File(servletHtmlPath));
        } catch (IOException e) {
            throw new ServletException(e);
        }
    }

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request wcstServlet request
     * @param response wcstServlet response
     * @throws ServletException if a wcstServlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("WCS-T: invoked with GET");
        printUsage(response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request wcstServlet request
     * @param response wcstServlet response
     * @throws ServletException if a wcstServlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Select the operation
        int op = -1;

        if (request.getParameter("TransactionXML") != null) {
            op = 4;
        }

        // Convert the WCS request into a WCPS request
        try {
            // initialize WebService operation arguments here
            String inputXml, outputXml;
            PrintWriter out;

            switch (op) {
                case 4:
                    inputXml = request.getParameter("TransactionXML");
                    outputXml = server.Transaction(inputXml);
                    out = new PrintWriter(response.getOutputStream());
                    out.write(outputXml);
                    out.flush();

                    break;
                default:
                    throw new Exception("No valid operation specified !");
            }

        } catch (Exception e) {
            e.printStackTrace();

            throw new ServletException("WCS-T servlet error !", e);
        }
    }

    /**
     * Returns a short description of the wcstServlet.
     * @return a String containing wcstServlet description
     */
    @Override
    public String getServletInfo() {
        return "WCS-T Extension. Petascope version " + ConfigManager.PETASCOPE_VERSION;
    }

    private void printUsage(HttpServletResponse response) throws IOException {
        System.out.println("WCS-T: returning usage message");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = new PrintWriter(response.getOutputStream());

        out.println(defaultHtmlResponse);

        out.close();
        System.out.println("WCS-T: done nothing");

    }
}