001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.restAPI;
023
024import java.io.IOException;
025import java.io.OutputStream;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.dom4j.dom.DOMDocument;
033import org.dom4j.dom.DOMDocumentFactory;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.io.download.DownloadService;
036import org.nuxeo.ecm.core.io.download.DownloadService.ByteRange;
037import org.nuxeo.runtime.api.Framework;
038import org.restlet.Request;
039import org.restlet.Response;
040import org.restlet.Restlet;
041import org.restlet.data.CharacterSet;
042import org.restlet.data.MediaType;
043import org.restlet.engine.adapter.HttpRequest;
044import org.restlet.engine.adapter.HttpResponse;
045import org.restlet.engine.adapter.ServerCall;
046import org.restlet.ext.servlet.internal.ServletCall;
047import org.restlet.representation.OutputRepresentation;
048import org.restlet.representation.Representation;
049import org.restlet.representation.StringRepresentation;
050import org.w3c.dom.Element;
051
052/**
053 * Base class for Nuxeo Restlet.
054 * <p>
055 * Provides utility methods:
056 * <ul>
057 * <li>error handling
058 * <li>authentication
059 * <li>http request/response retrieval
060 * </ul>
061 *
062 * @author tiry
063 * @deprecated since 10.3, will be removed
064 */
065@Deprecated
066public class BaseNuxeoRestlet extends Restlet {
067
068    private static final Log log = LogFactory.getLog(BaseNuxeoRestlet.class);
069
070    protected static boolean DEPRECATION_DONE;
071
072    protected void logDeprecation() {
073        if (!DEPRECATION_DONE) {
074            DEPRECATION_DONE = true;
075            log.warn("Restlet endpoints (" + getClass().getSimpleName()
076                    + ") are DEPRECATED since Nuxeo 10.3 and will be removed in a future version");
077        }
078    }
079
080    // error handling
081
082    protected static void handleError(Response res, String message) {
083        DOMDocumentFactory domFactory = new DOMDocumentFactory();
084        DOMDocument result = (DOMDocument) domFactory.createDocument();
085        handleError(result, res, message);
086    }
087
088    protected static void handleError(Response res, Exception e) {
089        DOMDocumentFactory domFactory = new DOMDocumentFactory();
090        DOMDocument result = (DOMDocument) domFactory.createDocument();
091        handleError(result, res, e.getMessage(), e.getClass().getCanonicalName());
092    }
093
094    protected static void handleError(DOMDocument result, Response res, Exception e) {
095        handleError(result, res, e.getMessage(), e.getClass().getCanonicalName());
096    }
097
098    protected static void handleError(DOMDocument result, Response res, String message) {
099        handleError(result, res, message, null);
100    }
101
102    private static void handleError(DOMDocument result, Response res, String message, String classMessage) {
103        Element error = result.createElement("error");
104        result.setRootElement((org.dom4j.Element) error);
105        error.setAttribute("message", message);
106        if (classMessage != null) {
107            error.setAttribute("class", classMessage);
108        }
109        result.setRootElement((org.dom4j.Element) error);
110
111        Representation rep = new StringRepresentation(result.asXML(), MediaType.APPLICATION_XML);
112        rep.setCharacterSet(CharacterSet.UTF_8);
113        res.setEntity(rep);
114    }
115
116    protected static HttpServletRequest getHttpRequest(Request req) {
117        HttpRequest httpRequest = (HttpRequest) req;
118        ServletCall httpCall = (ServletCall) httpRequest.getHttpCall();
119        return httpCall.getRequest();
120    }
121
122    protected static HttpServletResponse getHttpResponse(Response res) {
123        HttpResponse httpResponse = (HttpResponse) res;
124        ServletCall httpCall = (ServletCall) httpResponse.getHttpCall();
125        return httpCall.getResponse();
126    }
127
128    protected static String getRestletFullUrl(Request request) {
129        String url = getHttpRequest(request).getRequestURL().toString();
130        String qs = getHttpRequest(request).getQueryString();
131        if (qs != null) {
132            return url + '?' + qs;
133        } else {
134            return url;
135        }
136    }
137
138    protected static String getQueryParamValue(Request req, String paramName, String defaultValue) {
139        return req.getResourceRef().getQueryAsForm().getFirstValue(paramName, defaultValue);
140    }
141
142    /**
143     * Sets the response entity to a representation that will write the blob.
144     *
145     * @param blob the blob
146     * @param byteRange the byte range
147     * @param res the response
148     * @since 7.10
149     */
150    public void setEntityToBlobOutput(Blob blob, ByteRange byteRange, Response res) {
151        res.setEntity(new OutputRepresentation(null) {
152            @Override
153            public void write(OutputStream out) throws IOException {
154                DownloadService downloadService = Framework.getService(DownloadService.class);
155                downloadService.transferBlobWithByteRange(blob, byteRange, () -> out);
156            }
157        });
158    }
159
160}