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