|
15 | 15 | */
|
16 | 16 | package org.mvcspec.ozark.core;
|
17 | 17 |
|
| 18 | +import org.mvcspec.ozark.cdi.OzarkCdiExtension; |
18 | 19 | import org.mvcspec.ozark.engine.ViewEngineContextImpl;
|
19 | 20 | import org.mvcspec.ozark.engine.ViewEngineFinder;
|
| 21 | +import org.mvcspec.ozark.engine.Viewable; |
20 | 22 | import org.mvcspec.ozark.event.AfterProcessViewEventImpl;
|
21 | 23 | import org.mvcspec.ozark.event.BeforeProcessViewEventImpl;
|
22 |
| -import org.mvcspec.ozark.cdi.OzarkCdiExtension; |
23 | 24 |
|
24 | 25 | import javax.enterprise.event.Event;
|
25 | 26 | import javax.enterprise.inject.Instance;
|
26 | 27 | import javax.inject.Inject;
|
27 | 28 | import javax.mvc.Models;
|
28 | 29 | import javax.mvc.MvcContext;
|
29 |
| -import org.mvcspec.ozark.engine.Viewable; |
30 | 30 | import javax.mvc.engine.ViewEngine;
|
31 | 31 | import javax.mvc.engine.ViewEngineException;
|
32 | 32 | import javax.mvc.event.AfterProcessViewEvent;
|
|
52 | 52 | import java.io.OutputStreamWriter;
|
53 | 53 | import java.io.PrintWriter;
|
54 | 54 | import java.lang.annotation.Annotation;
|
| 55 | +import java.lang.reflect.InvocationTargetException; |
55 | 56 | import java.lang.reflect.Type;
|
56 | 57 | import java.nio.charset.Charset;
|
57 | 58 |
|
@@ -81,10 +82,10 @@ public class ViewableWriter implements MessageBodyWriter<Viewable> {
|
81 | 82 | private Instance<Models> modelsInstance;
|
82 | 83 |
|
83 | 84 | @Context
|
84 |
| - private HttpServletRequest request; |
| 85 | + private HttpServletRequest injectedRequest; |
85 | 86 |
|
86 | 87 | @Context
|
87 |
| - private HttpServletResponse response; |
| 88 | + private HttpServletResponse injectedResponse; |
88 | 89 |
|
89 | 90 | @Context
|
90 | 91 | private UriInfo uriInfo;
|
@@ -132,6 +133,10 @@ public void writeTo(Viewable viewable, Class<?> aClass, Type type, Annotation[]
|
132 | 133 | throw new ServerErrorException(messages.get("NoViewEngine", viewable), INTERNAL_SERVER_ERROR);
|
133 | 134 | }
|
134 | 135 |
|
| 136 | + // Special hack for WebSphere Liberty |
| 137 | + HttpServletRequest request = unwrap(injectedRequest, HttpServletRequest.class); |
| 138 | + HttpServletResponse response = unwrap(injectedResponse, HttpServletResponse.class); |
| 139 | + |
135 | 140 | // Create wrapper for response
|
136 | 141 | final ServletOutputStream responseStream = new DelegatingServletOutputStream(out);
|
137 | 142 | final PrintWriter responseWriter = new PrintWriter(new OutputStreamWriter(responseStream, getCharset(headers)));
|
@@ -174,6 +179,33 @@ public void writeTo(Viewable viewable, Class<?> aClass, Type type, Annotation[]
|
174 | 179 | }
|
175 | 180 | }
|
176 | 181 |
|
| 182 | + /** |
| 183 | + * This method is basically a dirty hack to get Ozark work on WebSphere Liberty. |
| 184 | + * The primary use case is to unwrap the original request/response from the wrapper we get |
| 185 | + * from CXF. This is required because the wrappers don't use the official wrapper base classes |
| 186 | + * and therefore Liberty fails to forward such requests because unwrapping isn't possible. |
| 187 | + */ |
| 188 | + private <T> T unwrap(T obj, Class<T> type) { |
| 189 | + |
| 190 | + String implName = obj.getClass().getName(); |
| 191 | + |
| 192 | + if (implName.equals("org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest") |
| 193 | + || implName.equals("org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletResponse")) { |
| 194 | + |
| 195 | + try { |
| 196 | + return type.cast( |
| 197 | + obj.getClass().getMethod("get").invoke(obj) |
| 198 | + ); |
| 199 | + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { |
| 200 | + throw new IllegalStateException("Failed to unwrap", e); |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + return obj; |
| 206 | + |
| 207 | + } |
| 208 | + |
177 | 209 | /**
|
178 | 210 | * Looks for a character set as part of the Content-Type header. Returns it
|
179 | 211 | * if specified or {@link #UTF8} if not.
|
|
0 commit comments