1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.admin.resources.representation.xml;
17
18 import java.io.OutputStream;
19 import java.io.PrintStream;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URI;
22 import java.util.List;
23
24 import javax.ws.rs.core.Link;
25
26 import org.apache.commons.lang3.StringEscapeUtils;
27
28 import de.smartics.properties.admin.domain.model.AppRelations;
29 import de.smartics.properties.admin.domain.model.PropertyDescriptors;
30 import de.smartics.properties.admin.resources.controller.PropertyDescriptorResource;
31 import de.smartics.properties.admin.resources.representation.share.BreadcrumbHelper;
32 import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
33 import de.smartics.properties.api.core.domain.PropertyDescriptor;
34 import de.smartics.properties.api.core.domain.PropertyKey;
35 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
36 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
37 import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
38 import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
39
40
41
42
43
44 public final class PropertyDescriptorsXmlRepresentationRenderer extends
45 AbstractXmlRepresentationRenderer<PropertyDescriptors>
46 {
47
48
49
50
51
52
53
54
55
56 private final PropertyDescriptors domainObject;
57
58
59
60
61
62
63
64
65
66
67
68
69 public PropertyDescriptorsXmlRepresentationRenderer(
70 final ResourceContext context, final PropertyDescriptors domainObject,
71 final OutputStream entityStream)
72 {
73 super(context, entityStream);
74 this.domainObject = domainObject;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 @Override
88 protected void httpBody() throws IllegalStateException
89 {
90 final String encoding = context.getResponse().getCharacterEncoding();
91 try
92 {
93 final PrintStream output = new PrintStream(entityStream, true, encoding);
94
95 output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
96 .append("\"?>\n\n");
97 output.append("<propertySets>\n");
98 for (final String propertySet : domainObject.getPropertyKeys())
99 {
100 final List<PropertyDescriptor> descriptors =
101 domainObject.get(propertySet);
102
103 output.append(" <propertySet id=\"")
104 .append(StringEscapeUtils.escapeXml(propertySet)).append("\">\n");
105 for (final PropertyDescriptor descriptor : descriptors)
106 {
107 output.append(" <descriptor>");
108 final PropertyKey key = descriptor.getKey();
109 output.append(StringEscapeUtils.escapeXml(key.toString()));
110 output.append("</descriptor>\n");
111 }
112 output.append(" </propertySet>\n");
113 }
114 output.append("</propertySets>\n");
115 }
116 catch (final UnsupportedEncodingException e)
117 {
118 throw new IllegalStateException(
119 "Cannot write descritors to XML file due to unsupported encoding '"
120 + encoding + "'.", e);
121 }
122 }
123
124 @Override
125 protected List<LinkDescriptor> hyperLinks()
126 {
127 final List<LinkDescriptor> links =
128 BreadcrumbHelper.adjustSelf(breadcrumb.descriptors());
129
130 for (final String propertySet : domainObject.getPropertyKeys())
131 {
132 final List<PropertyDescriptor> descriptors =
133 domainObject.get(propertySet);
134
135 for (final PropertyDescriptor descriptor : descriptors)
136 {
137 final PropertyKey key = descriptor.getKey();
138 final LinkDescriptor link = createLink(key.toString());
139 links.add(link);
140 }
141 }
142
143 return links;
144 }
145
146 private LinkDescriptor createLink(final String key)
147 {
148 final URI uri =
149 context.getUriInfo().getBaseUriBuilder()
150 .path(PropertyDescriptorResource.class, "getAsXml")
151 .build(new Object[] { key }, false);
152 final Link link =
153 Link.fromUri(uri).rel(AppRelations.DESCRIPTOR + " " + key).build();
154 final LinkMetadata metadata = new LinkMetadata();
155
156 final LinkDescriptorJaxrs descriptor =
157 new LinkDescriptorJaxrs(link, metadata);
158 return descriptor;
159 }
160
161 @Override
162 protected void close()
163 {
164 }
165
166
167
168 }