Project

General

Profile

Download (2.12 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id: RdfView.java 7479 2009-11-02 12:31:13Z ben.clark $
2
package eu.etaxonomy.cdm.remote.view;
3

    
4
import java.util.Map;
5

    
6
import javax.servlet.http.HttpServletRequest;
7
import javax.servlet.http.HttpServletResponse;
8
import javax.xml.transform.stream.StreamResult;
9

    
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Qualifier;
12
import org.springframework.oxm.Marshaller;
13
import org.springframework.web.servlet.view.AbstractView;
14

    
15
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
16

    
17
/**
18
 * View class which takes a serializes a cdm object as xml
19
 * @author ben
20
 * @see javax.xml.transform.Source
21
 * @see com.ibm.lsid.MetadataResponse
22
 */
23
public class XmlView extends AbstractView {
24

    
25
    private Marshaller marshaller;
26

    
27
    private boolean locationHeader = false;
28

    
29
    private String locationPrefix = "";
30

    
31
    public void setLocationHeader(boolean locationHeader) {
32
        this.locationHeader = locationHeader;
33
    }
34

    
35
    public void setLocationPrefix(String locationPrefix) {
36
        this.locationPrefix = locationPrefix;
37
    }
38

    
39
    public XmlView() {
40

    
41
    }
42

    
43
    @Autowired
44
    @Qualifier("marshaller")
45
    public void setMarshaller(Marshaller marshaller) {
46
        this.marshaller = marshaller;
47
    }
48

    
49

    
50

    
51
    @Override
52
    protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
53
            throws Exception {
54

    
55
        for(Object object : model.values()) {
56
            if(object instanceof IdentifiableEntity) {
57
                IdentifiableEntity identifiableEntity = (IdentifiableEntity)object;
58
                if(locationHeader) {
59
                    response.addHeader("Location", locationPrefix + identifiableEntity.getUuid().toString());
60
                }
61
                marshaller.marshal(identifiableEntity, new StreamResult(response.getOutputStream()));
62
            } else if(object instanceof Throwable) {
63
                eu.etaxonomy.cdm.io.jaxb.Error error = new eu.etaxonomy.cdm.io.jaxb.Error((Throwable)object);
64
                marshaller.marshal(error, new StreamResult(response.getOutputStream()));
65
            }
66
        }
67
    }
68

    
69
}
(10-10/10)