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.FileNotFoundException;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.PathParam;
027import javax.ws.rs.core.Response;
028import javax.ws.rs.core.Response.ResponseBuilder;
029
030import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Server Entry point to a server GWT module. Must be extended by the webengine resource used to load the studio
035 * application. The <code>@GET</code> method must be defined by the subclasses to point to the main HTML file of the GWT
036 * module. Example:
037 *
038 * <pre>
039 *     <code>@GET</code> <code>@Produces("text/html")</code>
040 *     public Object getIndex() {
041 *         return getTemplate("index.ftl");
042 *     }
043 * </pre>
044 *
045 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
046 */
047public abstract class GwtResource extends ModuleRoot {
048
049    /**
050     * Gets a static resource from the GWT module.
051     * @throws FileNotFoundException
052     */
053    @GET
054    @Path("{path:.*}")
055    public Response getResource(@PathParam("path") String path) throws FileNotFoundException {
056        ctx.getRequest().setAttribute("org.nuxeo.webengine.DisableAutoHeaders", "true");
057        File file = Framework.getService(GwtResolver.class).resolve(path);
058        if (file != null && file.isFile()) {
059            ResponseBuilder resp = Response.ok(file);
060            String fpath = file.getPath();
061            int p = fpath.lastIndexOf('.');
062            String ext = "";
063            if (p > -1) {
064                ext = fpath.substring(p + 1);
065            }
066            String mimeType = ctx.getEngine().getMimeType(ext);
067            if (mimeType == null) {
068                mimeType = "text/plain";
069            }
070            resp.type(mimeType);
071            return resp.build();
072        }
073        return Response.status(404).build();
074    }
075
076}