View Javadoc

1   /*******************************************************************************
2    *  Imixs Workflow 
3    *  Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,  
4    *  http://www.imixs.com
5    *  
6    *  This program is free software; you can redistribute it and/or 
7    *  modify it under the terms of the GNU General Public License 
8    *  as published by the Free Software Foundation; either version 2 
9    *  of the License, or (at your option) any later version.
10   *  
11   *  This program is distributed in the hope that it will be useful, 
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
14   *  General Public License for more details.
15   *  
16   *  You can receive a copy of the GNU General Public
17   *  License at http://www.gnu.org/licenses/gpl.html
18   *  
19   *  Project: 
20   *  	http://www.imixs.org
21   *  	http://java.net/projects/imixs-workflow
22   *  
23   *  Contributors:  
24   *  	Imixs Software Solutions GmbH - initial API and implementation
25   *  	Ralph Soika - Software Developer
26   *******************************************************************************/
27  
28  package org.imixs.workflow.jaxrs;
29  
30  import java.io.BufferedWriter;
31  import java.io.IOException;
32  import java.io.OutputStream;
33  import java.io.OutputStreamWriter;
34  import java.lang.annotation.Annotation;
35  import java.lang.reflect.Type;
36  
37  import javax.ws.rs.Produces;
38  import javax.ws.rs.WebApplicationException;
39  import javax.ws.rs.core.MediaType;
40  import javax.ws.rs.core.MultivaluedMap;
41  import javax.ws.rs.ext.MessageBodyWriter;
42  import javax.ws.rs.ext.Provider;
43  
44  import org.imixs.workflow.xml.EntityCollection;
45  import org.imixs.workflow.xml.XMLItemCollection;
46  
47  /**
48   * This MessageBodyWriter generates an HTML representation from a Entitycollection
49   * 
50   * @author rsoika
51   *
52   */
53  @Provider
54  @Produces("text/html")
55  public class EntityCollectionWriter implements MessageBodyWriter<EntityCollection> {
56  
57  	public boolean isWriteable(Class<?> type, Type genericType,
58  			Annotation[] annotations, MediaType mediaType) {
59  		return EntityCollection.class.isAssignableFrom(type);
60  	}
61  
62  	public void writeTo(EntityCollection entityCollection, Class<?> type, Type genericType,
63  			Annotation[] annotations, MediaType mediaType,
64  			MultivaluedMap<String, Object> httpHeaders,
65  			OutputStream entityStream) throws IOException,
66  			WebApplicationException {
67  		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
68  				entityStream));
69  		
70  		
71  		
72  		bw.write("<html>");
73  		XMLItemCollectionWriter.printHead(bw);
74  		bw.write("<body>");
75  		try {
76  			bw.write("<h1>EntityCollection</h1>");
77  			bw.write("<h2>" + entityCollection.getEntity().length + " Entries</h2>");
78  
79  			for (XMLItemCollection xmlworkItem : entityCollection.getEntity()) {
80  				XMLItemCollectionWriter.printXMLItemCollectionHTML(bw, xmlworkItem);
81  
82  			}
83  		} catch (Exception e) {
84  			bw.write("ERROR<br>");
85  			//e.printStackTrace(bw.);
86  		}
87  		
88  
89  		bw.write("</body>");
90  		bw.write("</html>");
91  		
92  		bw.flush();
93  	}
94  
95  	public long getSize(EntityCollection arg0, Class<?> arg1, Type arg2,
96  			Annotation[] arg3, MediaType arg4) {
97  		return -1;
98  	}
99  
100 	
101 	
102 	
103 
104 	
105 	
106 	
107 }