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.util.List;
22
23 import org.apache.commons.lang3.StringEscapeUtils;
24
25 import de.smartics.properties.admin.domain.model.ApiServices;
26 import de.smartics.properties.admin.domain.model.ApiServices.ServiceDescriptor;
27 import de.smartics.properties.admin.domain.model.AppRelations;
28 import de.smartics.properties.admin.resources.controller.ApiResource;
29 import de.smartics.properties.admin.resources.representation.share.BreadcrumbHelper;
30 import de.smartics.properties.admin.resources.representation.xml.share.AbstractXmlRepresentationRenderer;
31 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
32 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs;
33 import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
34 import de.smartics.resteasy.hypermedia.renderer.ResourceContext;
35
36
37
38
39 public final class ApiServicesXmlRepresentationRenderer extends
40 AbstractXmlRepresentationRenderer<ApiServices>
41 {
42
43
44
45
46
47
48
49
50
51 private final ApiServices domainObject;
52
53
54
55
56
57
58
59
60
61
62
63
64 public ApiServicesXmlRepresentationRenderer(final ResourceContext context,
65 final ApiServices domainObject, final OutputStream entityStream)
66 {
67 super(context, entityStream);
68 this.domainObject = domainObject;
69 }
70
71
72
73
74
75
76
77
78
79
80
81 @Override
82 protected void httpBody() throws IllegalStateException
83 {
84 final String encoding = context.getResponse().getCharacterEncoding();
85 try
86 {
87 final PrintStream output = new PrintStream(entityStream, true, encoding);
88 output.append("<?xml version=\"1.0\" encoding=\"").append(encoding)
89 .append("\"?>\n\n");
90
91 output.append("<services>\n");
92 for (final ServiceDescriptor service : domainObject.getServices())
93 {
94 if (service == ApiResource.SECURITY || service == ApiResource.JNDI)
95 {
96 continue;
97 }
98 final String id = service.getId();
99 output.append(" <service id=\"")
100 .append(StringEscapeUtils.escapeXml(id)).append("\">\n");
101 }
102 output.append("</services>\n");
103 }
104 catch (final UnsupportedEncodingException e)
105 {
106 throw new IllegalStateException(
107 "Cannot write services to XML file due to unsupported encoding '"
108 + encoding + "'.", e);
109 }
110 }
111
112 @Override
113 protected List<LinkDescriptor> hyperLinks()
114 {
115 final List<LinkDescriptor> links =
116 BreadcrumbHelper.adjustSelf(breadcrumb.api());
117
118 for (final ServiceDescriptor service : domainObject.getServices())
119 {
120 if (service == ApiResource.SECURITY || service == ApiResource.JNDI)
121 {
122 continue;
123 }
124
125 final LinkDescriptor link = createLink(service);
126 links.add(link);
127 }
128
129 return links;
130 }
131
132 private LinkDescriptor createLink(final ServiceDescriptor service)
133 {
134 final LinkDescriptor link =
135 linkDescriptorFactory.createDescriptor(service, "getAsXml");
136 link.addRels(getRelByService(service), service.getId());
137
138 final LinkMetadata metadata = new LinkMetadata();
139
140 final LinkDescriptorJaxrs descriptor =
141 new LinkDescriptorJaxrs(link, metadata);
142 return descriptor;
143 }
144
145 private String getRelByService(final ServiceDescriptor service)
146 {
147 if (service == ApiResource.CONFIGURATIONS)
148 {
149 return AppRelations.CONFIGURATIONS;
150 }
151 else if (service == ApiResource.DESCRIPTORS)
152 {
153 return AppRelations.DESCRIPTORS;
154 }
155
156 return AppRelations.API_HOME;
157 }
158
159 @Override
160 protected void close()
161 {
162 }
163
164
165
166 }