forked from zalando/logbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial sketch of a solution for zalando#172. We do not have a good w…
…ay of testing against 2.5 in the build yet, suggestions welcome :)
- Loading branch information
Team Mongoose
committed
Aug 22, 2017
1 parent
2497ce3
commit fbf1686
Showing
9 changed files
with
253 additions
and
24 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
logbook-servlet/src/main/java/org/zalando/logbook/servlet/AsyncHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.zalando.logbook.servlet; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Reflection here is used to support both Servlet API 2.5 and 3.0 in the same code base. | ||
*/ | ||
public class AsyncHelper { | ||
|
||
public static boolean isFirstRequest(HttpServletRequest request) { | ||
return dispatcherTypeClass() | ||
.map(theClass -> isAsyncDispatcherType(request, theClass)) | ||
.orElse(true); | ||
} | ||
|
||
public static boolean isLastRequest(final HttpServletRequest request) { | ||
return dispatcherTypeClass().isPresent() && !isAsyncStarted(request); | ||
} | ||
|
||
public static void setDispatcherTypeAsync(final HttpServletRequest request) { | ||
dispatcherTypeClass().ifPresent(dispatcherTypeClass -> { | ||
Object asyncDispatcherType = enumConstant(dispatcherTypeClass, "ASYNC"); | ||
invoke(request, "setDispatcherType", void.class, new Object[]{asyncDispatcherType}, dispatcherTypeClass); | ||
}); | ||
} | ||
|
||
private static boolean isAsyncDispatcherType(HttpServletRequest request, Class<?> dispatcherTypeClass) { | ||
Object dispatcherType = invoke(request, "getDispatcherType", Object.class, new Object[]{}); | ||
Object asyncDispatcherType = enumConstant(dispatcherTypeClass, "ASYNC"); | ||
return !Objects.equals(dispatcherType, asyncDispatcherType); | ||
} | ||
|
||
private static boolean isAsyncStarted(HttpServletRequest request) { | ||
return invoke(request, "isAsyncStarted", Boolean.class, new Object[]{}); | ||
} | ||
|
||
private static <T> T invoke(HttpServletRequest request, String methodName, Class<T> returnType, Object[] arguments, Class<?>... parameterTypes) { | ||
try { | ||
return returnType.cast(request.getClass().getMethod(methodName, parameterTypes).invoke(request, arguments)); | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private static Object enumConstant(Class<?> dispatcherType, String name) { | ||
return Arrays.stream(dispatcherType.getEnumConstants()) | ||
.filter(constant -> constant.toString().equals(name)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException("Could not find DispatcherType." + name)); | ||
} | ||
|
||
private static Optional<Class<?>> dispatcherTypeClass() { | ||
try { | ||
return Optional.of(Class.forName("javax.servlet.DispatcherType")); | ||
} catch (ClassNotFoundException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters