001/*
002 * (C) Copyright 2006-2011 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.app.jersey;
020
021import java.io.IOException;
022import java.util.Collections;
023import java.util.Enumeration;
024import java.util.Hashtable;
025import java.util.LinkedList;
026import java.util.List;
027import java.util.NoSuchElementException;
028
029import javax.servlet.ServletConfig;
030import javax.servlet.ServletException;
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpServletRequestWrapper;
033import javax.servlet.http.HttpServletResponse;
034import javax.ws.rs.core.HttpHeaders;
035
036import org.nuxeo.ecm.webengine.WebException;
037import org.nuxeo.ecm.webengine.jaxrs.Activator;
038import org.nuxeo.ecm.webengine.jaxrs.servlet.ApplicationServlet;
039
040/**
041 * WebEngine integration with OSGi JAX-RS model from ECR.
042 *
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045public class WebEngineServlet extends ApplicationServlet {
046
047    protected final String MIME_TYPE = "X-File-Type";
048
049    private final class DefaultContentTypeRequestWrapper extends HttpServletRequestWrapper {
050
051        protected final Hashtable<String, String[]> headers;
052
053        protected final String lCONTENT_TYPE = HttpHeaders.CONTENT_TYPE.toLowerCase();
054
055        protected final String lFILE_TYPE = MIME_TYPE.toLowerCase();
056
057        protected DefaultContentTypeRequestWrapper(HttpServletRequest request, boolean patchCType, boolean patchMType) {
058            super(request);
059            headers = patchHeaders(request, patchCType, patchMType);
060        }
061
062        protected Hashtable<String, String[]> patchHeaders(HttpServletRequest request, boolean patchCType,
063                boolean patchMType) {
064            Hashtable<String, String[]> headers = new Hashtable<String, String[]>();
065            // collect headers from request
066            Enumeration<String> eachNames = request.getHeaderNames();
067            while (eachNames.hasMoreElements()) {
068                String name = eachNames.nextElement().toLowerCase();
069                List<String> values = new LinkedList<String>();
070                Enumeration<String> eachValues = request.getHeaders(name);
071                while (eachValues.hasMoreElements()) {
072                    values.add(eachValues.nextElement());
073                }
074                headers.put(name, values.toArray(new String[values.size()]));
075            }
076            if (patchCType) {
077                // patch content type
078                String ctype = request.getContentType();
079                if (ctype == null || ctype.isEmpty()) {
080                    String[] ctypes = new String[] { "application/octet-stream" };
081                    headers.put(lCONTENT_TYPE, ctypes);
082                } else {
083                    patchContentTypes(headers.get(lCONTENT_TYPE));
084                }
085            }
086            if (patchMType) {
087                patchContentTypes(headers.get(lFILE_TYPE));
088            }
089            return headers;
090        }
091
092        protected void patchContentTypes(String[] ctypes) {
093            for (int index = 0; index < ctypes.length; ++index) {
094                String value = ctypes[index];
095                if (value.isEmpty()) {
096                    ctypes[index] = "application/octet-stream";
097                } else if (!value.contains("/")) {
098                    ctypes[index] = "application/".concat(value);
099                }
100            }
101        }
102
103        @Override
104        public Enumeration<String> getHeaderNames() {
105            return headers.keys();
106        }
107
108        @Override
109        public String getHeader(String name) {
110            String lname = name.toLowerCase();
111            if (!headers.containsKey(lname)) {
112                return null;
113            }
114            return headers.get(lname)[0];
115        }
116
117        @Override
118        public Enumeration<String> getHeaders(final String name) {
119            final String lname = name.toLowerCase();
120            if (!headers.containsKey(lname)) {
121                return Collections.emptyEnumeration();
122            }
123            return new Enumeration<String>() {
124                String[] values = headers.get(lname);
125
126                int index = 0;
127
128                @Override
129                public boolean hasMoreElements() {
130                    return index < values.length;
131                }
132
133                @Override
134                public String nextElement() {
135                    if (index >= values.length) {
136                        throw new NoSuchElementException(index + " is higher than " + values.length);
137                    }
138                    return values[index++];
139                }
140            };
141        }
142
143    }
144
145    private static final long serialVersionUID = 1L;
146
147    @Override
148    public void init(ServletConfig config) throws ServletException {
149        bundle = Activator.getInstance().getContext().getBundle();
150        super.init(config);
151    }
152
153    @Override
154    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
155        containerService(request, response);
156    }
157
158    @Override
159    protected void containerService(HttpServletRequest request, HttpServletResponse response) throws ServletException,
160            IOException {
161        if (isDirty) {
162            reloadContainer();
163        }
164        String method = request.getMethod().toUpperCase();
165        if (!"GET".equals(method)) {
166            // force reading properties because jersey is consuming one
167            // character
168            // from the input stream - see WebComponent.isEntityPresent.
169            request.getParameterMap();
170        }
171        final String ctype = request.getHeader(HttpHeaders.CONTENT_TYPE);
172        final String mtype = request.getHeader(MIME_TYPE);
173        boolean patchCType = ctype == null || ctype.length() == 0 || !ctype.contains("/");
174        boolean patchMMType = mtype != null && !mtype.contains("/");
175        if (patchCType || patchMMType) {
176            request = new DefaultContentTypeRequestWrapper(request, patchCType, patchMMType);
177        }
178        container.service(request, response);
179    }
180}