001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.restAPI;
021
022import java.security.Principal;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.dom4j.dom.DOMDocument;
028import org.dom4j.dom.DOMDocumentFactory;
029import org.restlet.Restlet;
030import org.restlet.data.CharacterSet;
031import org.restlet.data.MediaType;
032import org.restlet.data.Request;
033import org.restlet.data.Response;
034import org.restlet.resource.Representation;
035import org.restlet.resource.StringRepresentation;
036import org.w3c.dom.Element;
037
038import com.noelios.restlet.ext.servlet.ServletCall;
039import com.noelios.restlet.http.HttpCall;
040import com.noelios.restlet.http.HttpRequest;
041import com.noelios.restlet.http.HttpResponse;
042
043/**
044 * Base class for Nuxeo Restlet.
045 * <p>
046 * Provides utility methods:
047 * <ul>
048 * <li>error handling
049 * <li>authentication
050 * <li>http request/response retrieval
051 * </ul>
052 *
053 * @author tiry
054 */
055public class BaseNuxeoRestlet extends Restlet {
056
057    // error handling
058
059    protected static void handleError(Response res, String message) {
060        DOMDocumentFactory domFactory = new DOMDocumentFactory();
061        DOMDocument result = (DOMDocument) domFactory.createDocument();
062        handleError(result, res, message);
063    }
064
065    protected static void handleError(Response res, Exception e) {
066        DOMDocumentFactory domFactory = new DOMDocumentFactory();
067        DOMDocument result = (DOMDocument) domFactory.createDocument();
068        handleError(result, res, e.getMessage(), e.getClass().getCanonicalName());
069    }
070
071    protected static void handleError(DOMDocument result, Response res, Exception e) {
072        handleError(result, res, e.getMessage(), e.getClass().getCanonicalName());
073    }
074
075    protected static void handleError(DOMDocument result, Response res, String message) {
076        handleError(result, res, message, null);
077    }
078
079    private static void handleError(DOMDocument result, Response res, String message, String classMessage) {
080        Element error = result.createElement("error");
081        result.setRootElement((org.dom4j.Element) error);
082        error.setAttribute("message", message);
083        if (classMessage != null) {
084            error.setAttribute("class", classMessage);
085        }
086        result.setRootElement((org.dom4j.Element) error);
087
088        Representation rep = new StringRepresentation(result.asXML(), MediaType.APPLICATION_XML);
089        rep.setCharacterSet(CharacterSet.UTF_8);
090        res.setEntity(rep);
091    }
092
093    protected static HttpServletRequest getHttpRequest(Request req) {
094        if (req instanceof HttpRequest) {
095            HttpRequest httpRequest = (HttpRequest) req;
096            HttpCall httpCall = httpRequest.getHttpCall();
097            if (httpCall instanceof ServletCall) {
098                return ((ServletCall) httpCall).getRequest();
099            }
100        }
101        return null;
102    }
103
104    protected static HttpServletResponse getHttpResponse(Response res) {
105        if (res instanceof HttpResponse) {
106            HttpResponse httpResponse = (HttpResponse) res;
107            HttpCall httpCall = httpResponse.getHttpCall();
108            if (httpCall instanceof ServletCall) {
109                return ((ServletCall) httpCall).getResponse();
110            }
111        }
112        return null;
113    }
114
115    protected static Principal getUserPrincipal(Request req) {
116        HttpServletRequest httpServletRequest = getHttpRequest(req);
117        if (httpServletRequest == null) {
118            return null;
119        }
120        return httpServletRequest.getUserPrincipal();
121    }
122
123    protected static String getRestletFullUrl(Request request) {
124        String url = getHttpRequest(request).getRequestURL().toString();
125        String qs = getHttpRequest(request).getQueryString();
126        if (qs != null) {
127            return url + '?' + qs;
128        } else {
129            return url;
130        }
131    }
132
133    protected static String getQueryParamValue(Request req, String paramName, String defaultValue) {
134        return req.getResourceRef().getQueryAsForm().getFirstValue(paramName, defaultValue);
135    }
136
137}