summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/server/ApplicationRunnerServlet.java
blob: 774b866a445e42194bef22c1dc1ad5e15b38c9df (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
163
164
165
166
167
168
169
170
171
172
173
package com.vaadin.terminal.gwt.server;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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

import com.vaadin.Application;

@SuppressWarnings("serial")
public class ApplicationRunnerServlet extends AbstractApplicationServlet {

    /**
     * The name of the application class currently used. Only valid within one
     * request.
     */
    String applicationClassName = "";

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
    }

    @Override
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        applicationClassName = getApplicationRunnerApplicationClassName(request);
        super.service(request, response);
        applicationClassName = "";

    }

    @Override
    URL getApplicationUrl(HttpServletRequest request)
            throws MalformedURLException {
        URL url = super.getApplicationUrl(request);

        String path = url.toString();
        path += applicationClassName;
        path += "/";

        return new URL(path);
    }

    @Override
    protected Application getNewApplication(HttpServletRequest request)
            throws ServletException {

        // Creates a new application instance
        try {
            Class<?> applicationClass = getClassLoader().loadClass(
                    applicationClassName);
            final Application application = (Application) applicationClass
                    .newInstance();

            return application;
        } catch (final IllegalAccessException e) {
            throw new ServletException(e);
        } catch (final InstantiationException e) {
            throw new ServletException(e);
        } catch (final ClassNotFoundException e) {
            throw new ServletException(
                    new InstantiationException(
                            "Failed to load application class: "
                                    + applicationClassName));
        }

    }

    private String getApplicationRunnerApplicationClassName(
            HttpServletRequest request) {
        return getApplicationRunnerURIs(request).applicationClassname;
    }

    private static class URIS {
        String widgetsetPath;
        String applicationURI;
        String context;
        String runner;
        String applicationClassname;

    }

    /**
     * Parses application runner URIs.
     * 
     * If request URL is e.g.
     * http://localhost:8080/itmill/run/com.vaadin.demo.Calc then
     * <ul>
     * <li>context=itmill</li>
     * <li>Runner servlet=run</li>
     * <li>Toolkit application=com.vaadin.demo.Calc</li>
     * </ul>
     * 
     * @param request
     * @return string array containing widgetset URI, application URI and
     *         context, runner, application classname
     */
    private static URIS getApplicationRunnerURIs(HttpServletRequest request) {
        final String[] urlParts = request.getRequestURI().toString().split(
                "\\/");
        String context = null;
        String runner = null;
        URIS uris = new URIS();
        String applicationClassname = null;
        String contextPath = request.getContextPath();
        if (urlParts[1].equals(contextPath.replaceAll("\\/", ""))) {
            // class name comes after web context and runner application
            context = urlParts[1];
            runner = urlParts[2];
            if (urlParts.length == 3) {
                throw new IllegalArgumentException("No application specified");
            }
            applicationClassname = urlParts[3];

            uris.widgetsetPath = "/" + context;
            uris.applicationURI = "/" + context + "/" + runner + "/"
                    + applicationClassname;
            uris.context = context;
            uris.runner = runner;
            uris.applicationClassname = applicationClassname;
        } else {
            // no context
            context = "";
            runner = urlParts[1];
            if (urlParts.length == 2) {
                throw new IllegalArgumentException("No application specified");
            }
            applicationClassname = urlParts[2];

            uris.widgetsetPath = "/";
            uris.applicationURI = "/" + runner + "/" + applicationClassname;
            uris.context = context;
            uris.runner = runner;
            uris.applicationClassname = applicationClassname;
        }
        return uris;
    }

    // @Override
    @Override
    protected Class getApplicationClass() throws ClassNotFoundException {
        // TODO use getClassLoader() ?
        return getClass().getClassLoader().loadClass(applicationClassName);
    }

    @Override
    String getRequestPathInfo(HttpServletRequest request) {
        String path = request.getPathInfo();
        if (path == null) {
            return null;
        }

        path = path.substring(1 + applicationClassName.length());
        return path;
    }

    @Override
    String getWidgetsetLocation(HttpServletRequest request) {
        URIS uris = getApplicationRunnerURIs(request);
        String widgetsetPath = uris.widgetsetPath;
        if (widgetsetPath.equals("/")) {
            widgetsetPath = "";
        }

        return widgetsetPath;
    }

}