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.DocumentationItem; 039 040@Provider 041public class DocumentationItemReader implements MessageBodyReader<DocumentationItem> { 042 043 public static final MediaType DocumentationItemMediaType = new MediaType("application", "x-www-form-urlencoded"); 044 045 protected static final Log log = LogFactory.getLog(DocumentationItemReader.class); 046 047 @Context 048 protected HttpServletRequest request; 049 050 @Override 051 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 052 return DocumentationItemMediaType.equals(mediaType); 053 } 054 055 @Override 056 public DocumentationItem readFrom(Class<DocumentationItem> type, Type genericType, Annotation[] annotations, 057 MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) 058 throws IOException, WebApplicationException { 059 060 SimpleDocumentationItem item = new SimpleDocumentationItem(); 061 item.content = request.getParameter("content"); 062 item.id = request.getParameter("id"); 063 item.renderingType = request.getParameter("renderingType"); 064 item.target = request.getParameter("target"); 065 item.targetType = request.getParameter("targetType"); 066 item.title = request.getParameter("title"); 067 item.type = request.getParameter("type"); 068 item.uuid = request.getParameter("uuid"); 069 String v = request.getParameter("approved"); 070 if ("on".equals(v)) { // TODO better to use "true" or "false" and use 071 // Boolean.parseBoolean(v) to decode it 072 item.approved = true; 073 } 074 String[] versions = request.getParameterValues("versions"); 075 if (versions != null) { 076 for (String version : versions) { 077 item.applicableVersion.add(version); 078 } 079 } 080 081 String[] attachmentsTitles = request.getParameterValues("attachmentsTitle"); 082 if (attachmentsTitles != null && attachmentsTitles.length > 0) { 083 String[] attachmentsContents = request.getParameterValues("attachmentsContent"); 084 Map<String, String> attachments = new LinkedHashMap<String, String>(); 085 int idx = 0; 086 for (String attachmentsTitle : attachmentsTitles) { 087 if (attachmentsContents.length > idx) { 088 attachments.put(attachmentsTitle, attachmentsContents[idx]); 089 } 090 idx += 1; 091 } 092 item.attachments = attachments; 093 } 094 return item; 095 } 096 097}