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.ConfigurationKeys;
30 import de.smartics.properties.admin.resources.controller.PropertyKeysResource;
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.config.domain.key.ConfigurationKey;
34 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
35 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
36 import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
37 import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
38
39
40
41
42 public final class ConfigurationKeysXmlRepresentationRenderer extends
43 AbstractXmlRepresentationRenderer<ConfigurationKeys>
44 {
45
46
47
48
49
50
51
52
53
54 private final ConfigurationKeys domainObject;
55
56
57
58
59
60
61
62
63
64
65
66
67 public ConfigurationKeysXmlRepresentationRenderer(
68 final ResourceContext context, final ConfigurationKeys domainObject,
69 final OutputStream entityStream)
70 {
71 super(context, entityStream);
72 this.domainObject = domainObject;
73 }
74
75
76
77
78
79
80
81
82
83
84
85 @Override
86 protected void httpBody() throws IllegalStateException
87 {
88 final String encoding = context.getResponse().getCharacterEncoding();
89 try
90 {
91 final PrintStream output = new PrintStream(entityStream, true, encoding);
92 output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
93 .append("\"?>\n\n");
94
95 output.append("<configurations>\n");
96 for (final ConfigurationKey<?> key : domainObject.getKeys())
97 {
98 final String keyString = key.toString();
99 output.append(" <configuration id=\"")
100 .append(StringEscapeUtils.escapeXml(keyString)).append("\">\n");
101 }
102
103 output.append("</configurations>\n");
104 }
105 catch (final UnsupportedEncodingException e)
106 {
107 throw new IllegalStateException(
108 "Cannot write configurations to XML file due to unsupported encoding '"
109 + encoding + "'.", e);
110 }
111 }
112
113 @Override
114 protected List<LinkDescriptor> hyperLinks()
115 {
116 final List<LinkDescriptor> links =
117 BreadcrumbHelper.adjustSelf(breadcrumb.configurations());
118
119 for (final ConfigurationKey<?> key : domainObject.getKeys())
120 {
121 final LinkDescriptor link = createLink(key.toString());
122 links.add(link);
123 }
124
125 return links;
126 }
127
128 private LinkDescriptor createLink(final String key)
129 {
130 final URI uri =
131 context.getUriInfo().getBaseUriBuilder()
132 .path(PropertyKeysResource.class, "getAsXml")
133 .build(new Object[] { key.toString() }, false);
134 final Link link =
135 Link.fromUri(uri).rel(AppRelations.CONFIGURATION + " " + key).build();
136 final LinkMetadata metadata = new LinkMetadata();
137
138 final LinkDescriptorJaxrs descriptor =
139 new LinkDescriptorJaxrs(link, metadata);
140 return descriptor;
141 }
142
143 @Override
144 protected void close()
145 {
146 }
147
148
149
150 }