Project

General

Profile

Download (4.53 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.view;
10

    
11

    
12

    
13
import java.util.Collection;
14
import java.util.HashMap;
15
import java.util.Iterator;
16
import java.util.Map;
17

    
18

    
19

    
20
import javax.servlet.http.HttpServletRequest;
21
import javax.servlet.http.HttpServletResponse;
22
import javax.xml.transform.stream.StreamResult;
23

    
24
import org.dozer.Mapper;
25
import org.joda.time.DateTime;
26
import org.springframework.beans.factory.annotation.Autowired;
27
import org.springframework.beans.factory.annotation.Qualifier;
28
import org.springframework.oxm.Marshaller;
29
import org.springframework.web.servlet.view.AbstractView;
30

    
31
import com.ibm.lsid.http.HTTPConstants;
32

    
33
import eu.etaxonomy.cdm.remote.dto.common.RemoteResponse;
34
import eu.etaxonomy.cdm.remote.dto.tdwg.BaseThing;
35
import eu.etaxonomy.remote.dto.rdf.Rdf;
36

    
37
/**
38
 * This class handles rdf views by mapping data objects to corresponding rdf objects, which are later 
39
 * run through a marshaller to produce xml or json 
40
 * 
41
 * @author b.clarke,c.mathew
42
 * @version 1.0.0
43
 * @created 25-Nov-2012
44
 */
45

    
46
public class RdfView extends AbstractView {
47
	
48
	//FIXME : the standard marshaller as defined in remote.xml is not used in web service views so it
49
	//is commented aout for the moment.
50
	
51
//	private Marshaller marshaller;
52
	
53
	private Marshaller rdfMarshaller;
54
	
55
	private Mapper mapper;
56
	
57
//	private Map<Class<? extends CdmBase>,Class<? extends BaseThing>> classMap = new HashMap<Class<? extends CdmBase>,Class<? extends BaseThing>>();
58
	private Map<Class<? extends RemoteResponse>,Class<? extends BaseThing>> remoteClassMap = new HashMap<Class<? extends RemoteResponse>,Class<? extends BaseThing>>();
59
	
60
	private Integer expiresPlus;
61
	
62
    public enum Type{
63
        RDFXML("application/rdf+xml"),
64
        RDFJSON("application/rdf+json");
65

    
66
        private final String contentType;
67

    
68
        Type(String contentType){
69
            this.contentType = contentType;
70
        }
71

    
72
        public String getContentType(){
73
            return contentType;
74
        }
75
    }
76

    
77
    private Type type = Type.RDFXML;
78

    
79

    
80
    public Type getType() {
81
        return type;
82
    }
83

    
84
    public void setType(Type type) {
85
        this.type = type;
86
    }
87
    
88
    /*
89
     * (non-Javadoc)
90
     * @see org.springframework.web.servlet.View#getContentType()
91
     */
92
    @Override
93
    public String getContentType() {
94
        return type.getContentType();
95
    }
96
    
97
	public RdfView() {
98
//		classMap.put(Taxon.class, TaxonConcept.class);
99
//		classMap.put(Synonym.class, TaxonConcept.class);
100
//		classMap.put(TaxonDescription.class, SpeciesProfileModel.class);
101
		
102
		remoteClassMap.put(eu.etaxonomy.cdm.remote.dto.namecatalogue.NameInformation.class, 
103
				eu.etaxonomy.cdm.remote.dto.cdm.NameInformationRdf.class);
104
	}
105
	
106
//	@Autowired
107
//	public void setMarshaller(Marshaller marshaller) {
108
//		this.marshaller = marshaller;
109
//	}
110
	
111
	@Autowired
112
	@Qualifier("rdfMarshaller")
113
	public void setRdfMarshaller(Marshaller rdfMarshaller) {
114
		this.rdfMarshaller = rdfMarshaller;
115
	}
116
	
117
	@Autowired
118
	public void setMapper(Mapper mapper) {
119
		this.mapper = mapper;
120
	}
121
	
122
	public void setExpiresPlus(Integer expiresPlus) {
123
		this.expiresPlus = expiresPlus;
124
	}
125

    
126
	@Override
127
	protected void renderMergedOutputModel(Map model,HttpServletRequest request, HttpServletResponse response)
128
			throws Exception {
129
		if(expiresPlus != null) {
130
		    DateTime expires = new DateTime();
131
	        response.setHeader(HTTPConstants.EXPIRES_HEADER, HTTPConstants.HTTP_DATE_FORMAT.format(expires.plusDays(expiresPlus).toDate()));
132
	    }
133
			
134
		rdfMarshaller.marshal(buildRdf(model), new StreamResult(response.getOutputStream()));
135
	}
136
	
137
	
138
	public Rdf buildRdf(Map model) {
139
		Rdf rdf = new Rdf();
140
		for(Object object : model.values()) {
141
//		    if(object instanceof IdentifiableEntity) {
142
//		        IdentifiableEntity identifiableEntity = (IdentifiableEntity)object;
143
//		        Class clazz = classMap.get(identifiableEntity.getClass());
144
//		        if(clazz != null) {
145
//		          rdf.addThing((BaseThing)mapper.map(identifiableEntity, clazz));
146
//		        }
147
//		    } else 
148
		    if(object instanceof Collection) {
149
		    	Collection c = (Collection)object;
150
		    	Iterator itr = c.iterator();
151
		    	while(itr.hasNext()) {		    		
152
		    		Object obj = itr.next();
153
		    		Class clazz = remoteClassMap.get(obj.getClass());
154
		    		if(clazz != null) {
155
		    			rdf.addThing((BaseThing)mapper.map(obj, clazz));
156

    
157
		    		}
158
		    	}
159
		    }
160
		} 	
161
		return rdf;
162
	}
163

    
164
}
(8-8/10)