001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.ecm.webengine.gwt;
018
019import java.io.File;
020import java.io.FileNotFoundException;
021
022import javax.ws.rs.GET;
023import javax.ws.rs.Path;
024import javax.ws.rs.PathParam;
025import javax.ws.rs.core.Response;
026import javax.ws.rs.core.Response.ResponseBuilder;
027
028import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Server Entry point to a server GWT module. Must be extended by the webengine resource used to load the studio
033 * application. The <code>@GET</code> method must be defined by the subclasses to point to the main HTML file of the GWT
034 * module. Example:
035 *
036 * <pre>
037 *     <code>@GET</code> <code>@Produces("text/html")</code>
038 *     public Object getIndex() {
039 *         return getTemplate("index.ftl");
040 *     }
041 * </pre>
042 *
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045public abstract class GwtResource extends ModuleRoot {
046
047    /**
048     * Gets a static resource from the GWT module.
049     * @throws FileNotFoundException
050     */
051    @GET
052    @Path("{path:.*}")
053    public Response getResource(@PathParam("path") String path) throws FileNotFoundException {
054        ctx.getRequest().setAttribute("org.nuxeo.webengine.DisableAutoHeaders", "true");
055        File file = Framework.getService(GwtResolver.class).resolve(path);
056        if (file != null && file.isFile()) {
057            ResponseBuilder resp = Response.ok(file);
058            String fpath = file.getPath();
059            int p = fpath.lastIndexOf('.');
060            String ext = "";
061            if (p > -1) {
062                ext = fpath.substring(p + 1);
063            }
064            String mimeType = ctx.getEngine().getMimeType(ext);
065            if (mimeType == null) {
066                mimeType = "text/plain";
067            }
068            resp.type(mimeType);
069            return resp.build();
070        }
071        return Response.status(404).build();
072    }
073
074}