001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.gwt;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.FileNotFoundException;
024import java.io.IOException;
025import java.net.MalformedURLException;
026import java.net.URL;
027import java.text.ParseException;
028
029import javax.servlet.ServletConfig;
030import javax.servlet.ServletException;
031import javax.servlet.http.HttpServletRequest;
032
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.nuxeo.ecm.core.api.NuxeoException;
036import org.nuxeo.runtime.api.Framework;
037
038import com.google.gwt.user.server.rpc.RemoteServiceServlet;
039import com.google.gwt.user.server.rpc.SerializationPolicy;
040import com.google.gwt.user.server.rpc.SerializationPolicyLoader;
041
042/**
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045public class WebEngineGwtServlet extends RemoteServiceServlet {
046
047    private static final long serialVersionUID = 1L;
048
049    private static Log log = LogFactory.getLog(WebEngineGwtServlet.class);
050
051
052    @Override
053    public void init(ServletConfig config) throws ServletException {
054        super.init(config);
055    }
056
057    /**
058     * When in hosted mode the default mechanism is used. In production mode the last path element from the request URL
059     * is considered as the GWT module identifier and the GWT application root will be resolved to
060     * <code>${nxserver}/web/root.war/gwt/gwtModuleId</code>
061     * <p>
062     * The GWT web application will be copied there at startup time by using the extension to
063     * {@link InstallGwtAppComponent} extension point <code>install</code>. in your GWT bundle.
064     *
065     * @see {@link #_doGetSerializationPolicy(HttpServletRequest, String, String)}
066     */
067    @Override
068    protected SerializationPolicy doGetSerializationPolicy(HttpServletRequest request, String moduleBaseURL,
069            String strongName) {
070        try {
071            return _doGetSerializationPolicy(request, moduleBaseURL, strongName);
072        } catch (FileNotFoundException cause) {
073            throw new NuxeoException("Cannot find serialization policy for " + moduleBaseURL, cause);
074        }
075    }
076
077    protected SerializationPolicy _doGetSerializationPolicy(HttpServletRequest request, String moduleBaseURL,
078            String strongName) throws FileNotFoundException  {
079
080        String modulePath = null;
081        if (moduleBaseURL != null) {
082            try {
083                modulePath = new URL(moduleBaseURL).getPath();
084            } catch (MalformedURLException ex) {
085                // log the information, we will default
086                log.warn("Malformed moduleBaseURL: " + moduleBaseURL, ex);
087                return super.doGetSerializationPolicy(request, moduleBaseURL, strongName);
088            }
089        }
090        String moduleId = new File(modulePath).getName();
091        if (moduleId.length() == 0) {
092            moduleId = "root";
093        }
094        String filename = SerializationPolicyLoader.getSerializationPolicyFileName(strongName);
095        File policyFile = Framework.getService(GwtResolver.class).resolve(moduleId+"/"+filename);
096        if (policyFile == null || !policyFile.isFile()) {
097            log.warn("Could not find gwt serialization policy file for module " + moduleId + " [ " + filename + " ]");
098            return super.doGetSerializationPolicy(request, moduleBaseURL, strongName);
099        }
100        log.debug("Found gwt serialization policy file: " + policyFile);
101        FileInputStream in = null;
102        try {
103            in = new FileInputStream(policyFile);
104            return SerializationPolicyLoader.loadFromStream(in, null);
105        } catch (IOException e) {
106            log.error("Failed to read gwt serialization policy file for module " + moduleId + " [ " + filename + " ]", e);
107            return super.doGetSerializationPolicy(request, moduleBaseURL, strongName);
108        } catch (ParseException e) {
109            log.error("Failed to parse the policy file '" + policyFile + "'", e);
110            return super.doGetSerializationPolicy(request, moduleBaseURL, strongName);
111        } finally {
112            if (in != null) {
113                try {
114                    in.close();
115                } catch (IOException e) {
116                    log.error(e);
117                }
118            }
119        }
120    }
121
122}