001/*
002 * (C) Copyright 2006-2010 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 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.doc;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.lang.annotation.Annotation;
024import java.lang.reflect.Type;
025import java.util.LinkedHashMap;
026import java.util.Map;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.ws.rs.WebApplicationException;
030import javax.ws.rs.core.Context;
031import javax.ws.rs.core.MediaType;
032import javax.ws.rs.core.MultivaluedMap;
033import javax.ws.rs.ext.MessageBodyReader;
034import javax.ws.rs.ext.Provider;
035
036import org.apache.commons.logging.Log;
037import org.apache.commons.logging.LogFactory;
038import org.nuxeo.apidoc.api.AbstractDocumentationItem;
039import org.nuxeo.apidoc.api.DocumentationItem;
040
041@Provider
042public class DocumentationItemReader implements MessageBodyReader<DocumentationItem> {
043
044    public static final MediaType DocumentationItemMediaType = new MediaType("application", "x-www-form-urlencoded");
045
046    protected static final Log log = LogFactory.getLog(DocumentationItemReader.class);
047
048    @Context
049    protected HttpServletRequest request;
050
051    @Override
052    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
053        return DocumentationItemMediaType.equals(mediaType);
054    }
055
056    @Override
057    public DocumentationItem readFrom(Class<DocumentationItem> type, Type genericType, Annotation[] annotations,
058            MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
059            throws IOException, WebApplicationException {
060
061        SimpleDocumentationItem item = new SimpleDocumentationItem(AbstractDocumentationItem.typeLabelOf(request.getParameter("type")));
062        item.content = request.getParameter("content");
063        item.id = request.getParameter("id");
064        item.renderingType = request.getParameter("renderingType");
065        item.target = request.getParameter("target");
066        item.targetType = request.getParameter("targetType");
067        item.title = request.getParameter("title");
068        item.type = request.getParameter("type");
069        item.uuid = request.getParameter("uuid");
070        String v = request.getParameter("approved");
071        if ("on".equals(v)) { // TODO better to use "true" or "false" and use
072                              // Boolean.parseBoolean(v) to decode it
073            item.approved = true;
074        }
075        String[] versions = request.getParameterValues("versions");
076        if (versions != null) {
077            for (String version : versions) {
078                item.applicableVersion.add(version);
079            }
080        }
081
082        String[] attachmentsTitles = request.getParameterValues("attachmentsTitle");
083        if (attachmentsTitles != null && attachmentsTitles.length > 0) {
084            String[] attachmentsContents = request.getParameterValues("attachmentsContent");
085            Map<String, String> attachments = new LinkedHashMap<>();
086            int idx = 0;
087            for (String attachmentsTitle : attachmentsTitles) {
088                if (attachmentsContents.length > idx) {
089                    attachments.put(attachmentsTitle, attachmentsContents[idx]);
090                }
091                idx += 1;
092            }
093            item.attachments = attachments;
094        }
095        return item;
096    }
097
098}