1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.resteasy.hypermedia.renderer;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.Type;
22 import java.util.Locale;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.ws.rs.WebApplicationException;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.MultivaluedMap;
30 import javax.ws.rs.core.UriInfo;
31 import javax.ws.rs.ext.MessageBodyWriter;
32
33 import de.smartics.resteasy.hypermedia.resources.ResourceDiscovery;
34 import de.smartics.resteasy.hypermedia.resources.Resources;
35
36
37
38
39
40
41
42 public abstract class AbstractRepresentationBodyWriter<T> implements
43 MessageBodyWriter<T>
44 {
45
46
47
48
49
50
51
52
53
54 @Context
55 private UriInfo uriInfo;
56
57
58
59
60 @Context
61 private HttpServletRequest httpRequest;
62
63
64
65
66 @Context
67 private HttpServletResponse httpResponse;
68
69
70
71
72
73
74
75
76 protected AbstractRepresentationBodyWriter()
77 {
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 protected abstract I18nProvider getI18nProvider();
94
95
96
97 @Override
98 public long getSize(final T t, final Class<?> type, final Type genericType,
99 final Annotation[] annotations, final MediaType mediaType)
100 {
101 return -1;
102 }
103
104
105 @Override
106 public void writeTo(final T t, final Class<?> type, final Type genericType,
107 final Annotation[] annotations, final MediaType mediaType,
108 final MultivaluedMap<String, Object> httpHeaders,
109 final OutputStream entityStream) throws IOException,
110 WebApplicationException
111 {
112 final LocalizationProvider l7nProvider = fetchLocalizationProvider();
113 final Resources discovery = ResourceDiscovery.createDiscovery(t, uriInfo);
114 final ResourceContext context =
115 new ResourceContext.Builder().with(httpRequest).with(httpResponse)
116 .with(uriInfo).with(l7nProvider).with(discovery).withType(type)
117 .withGenericType(genericType).with(annotations).with(mediaType)
118 .withHttpHeaders(httpHeaders).build();
119 final RepresentationRenderer<T> renderer = create(context, t, entityStream);
120 renderer.render();
121 }
122
123
124
125 private LocalizationProvider fetchLocalizationProvider()
126 {
127 final Locale locale = httpRequest.getLocale();
128 final LocalizationProvider l7nProvider =
129 getI18nProvider().getProvider(locale);
130 return l7nProvider;
131 }
132
133
134
135
136
137
138
139
140
141
142 protected abstract RepresentationRenderer<T> create(ResourceContext context,
143 T domainObject, OutputStream entityStream);
144
145
146
147 }