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.lang.ObjectUtils;
27 import org.apache.commons.lang3.StringEscapeUtils;
28
29 import de.smartics.properties.admin.domain.model.AppRelations;
30 import de.smartics.properties.admin.domain.model.PropertiesConfiguration;
31 import de.smartics.properties.admin.resources.controller.PropertyResource;
32 import de.smartics.properties.admin.resources.representation.share.BreadcrumbHelper;
33 import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
34 import de.smartics.properties.api.config.domain.DescribedProperty;
35 import de.smartics.properties.api.config.domain.ValidatedProperty;
36 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
37 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
38 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
39 import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
40 import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
41
42
43
44
45 public final class PropertyKeysXmlRepresentationRenderer extends
46 AbstractXmlRepresentationRenderer<PropertiesConfiguration>
47 {
48
49
50
51
52
53
54
55
56
57 private final PropertiesConfiguration domainObject;
58
59
60
61
62
63
64
65
66
67
68
69
70 public PropertyKeysXmlRepresentationRenderer(final ResourceContext context,
71 final PropertiesConfiguration domainObject,
72 final OutputStream entityStream)
73 {
74 super(context, entityStream);
75 this.domainObject = domainObject;
76 }
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 protected void httpBody() throws IllegalStateException
90 {
91 final String encoding = context.getResponse().getCharacterEncoding();
92 try
93 {
94 final PrintStream output = new PrintStream(entityStream, true, encoding);
95
96 output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
97 .append("\"?>\n\n");
98 output.append("<properties config=\"")
99 .append(StringEscapeUtils.escapeXml(domainObject.getKey()))
100 .append("\">\n");
101
102 for (final DescribedProperty property : domainObject.getProperties())
103 {
104 renderRow(output, property);
105 }
106 output.append("</properties>\n");
107 }
108 catch (final UnsupportedEncodingException e)
109 {
110 throw new IllegalStateException(
111 "Cannot write properties to XML file due to unsupported encoding '"
112 + encoding + "'.", e);
113 }
114 }
115
116 private void renderRow(final PrintStream output,
117 final DescribedProperty property)
118 {
119 final String value;
120 if (property instanceof ValidatedProperty)
121 {
122 value =
123 ObjectUtils.toString(((ValidatedProperty) property)
124 .getValidatedValue());
125 }
126 else
127 {
128 value = property.getValue();
129 }
130
131 final String key = property.getName();
132 final String source = createConfigSource(property);
133 output.append(" <property name=\"")
134 .append(StringEscapeUtils.escapeXml(key)).append("\" source=\"")
135 .append(StringEscapeUtils.escapeXml(source)).append("\">");
136
137 if (value != null)
138 {
139 output.append(StringEscapeUtils.escapeXml(value));
140 }
141 output.append("</property>\n");
142 }
143
144 private String createConfigSource(final DescribedProperty property)
145 {
146 final ConfigurationKey<?> configurationKey = property.getConfigurationKey();
147 final String label =
148 configurationKey == null ? "default" : configurationKey.toString();
149 return label;
150 }
151
152 @Override
153 protected List<LinkDescriptor> hyperLinks()
154 {
155 final List<LinkDescriptor> links =
156 BreadcrumbHelper.adjustSelf(breadcrumb.configuration(domainObject
157 .getKey()));
158
159 for (final DescribedProperty property : domainObject.getProperties())
160 {
161 final String name = property.getName();
162 final LinkDescriptor link = createLink(name);
163 links.add(link);
164 }
165
166 return links;
167 }
168
169 private LinkDescriptor createLink(final String propertyName)
170 {
171 final URI uri =
172 context.getUriInfo().getBaseUriBuilder()
173 .path(PropertyResource.class, "getAsXml")
174 .build(new Object[] { domainObject.getKey(), propertyName }, false);
175 final Link link =
176 Link.fromUri(uri).rel(AppRelations.PROPERTY + " " + propertyName)
177 .build();
178 final LinkMetadata metadata = new LinkMetadata();
179
180 final LinkDescriptorJaxrs descriptor =
181 new LinkDescriptorJaxrs(link, metadata);
182 return descriptor;
183 }
184
185 @Override
186 protected void close()
187 {
188 }
189
190
191
192 }