-
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.
#20 : Resource methods should support javax.ws.rs.core.Form
- Loading branch information
Showing
11 changed files
with
160 additions
and
10 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
core/src/main/java/net/cactusthorn/routing/invoke/FormParameter.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,58 @@ | ||
package net.cactusthorn.routing.invoke; | ||
|
||
import static net.cactusthorn.routing.util.Messages.Key.WRONG_CONTENT_TYPE; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.ws.rs.core.Form; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedHashMap; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import net.cactusthorn.routing.RoutingInitializationException; | ||
import net.cactusthorn.routing.uri.PathTemplate.PathValues; | ||
import net.cactusthorn.routing.util.Messages; | ||
|
||
public class FormParameter extends MethodParameter { | ||
|
||
// @formatter:off | ||
private static final Set<MediaType> CONTENT_TYPE = Collections | ||
.unmodifiableSet(new HashSet<>(Arrays.asList( | ||
new MediaType[] {MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.MULTIPART_FORM_DATA_TYPE}))); | ||
// @formatter:off | ||
|
||
public FormParameter(Method method, Parameter parameter, Type genericType, int position, Set<MediaType> consumesMediaTypes) { | ||
super(method, parameter, genericType, position); | ||
|
||
for (MediaType contentType : consumesMediaTypes) { | ||
if (!CONTENT_TYPE.contains(contentType)) { | ||
throw new RoutingInitializationException( | ||
Messages.msg(WRONG_CONTENT_TYPE, "javax.ws.rs.core.Form", CONTENT_TYPE, method())); | ||
} | ||
} | ||
} | ||
|
||
@Override // | ||
protected Form findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, PathValues pathValues) | ||
throws Exception { | ||
|
||
MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>(); | ||
for (Enumeration<String> e = req.getParameterNames(); e.hasMoreElements();) { | ||
String name = e.nextElement(); | ||
String[] values = req.getParameterValues(name); | ||
parameters.addAll(name, values); | ||
} | ||
return new Form(parameters); | ||
} | ||
|
||
} |
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
60 changes: 60 additions & 0 deletions
60
core/src/test/java/net/cactusthorn/routing/invoke/FormParameterTest.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,60 @@ | ||
package net.cactusthorn.routing.invoke; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.core.Form; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import net.cactusthorn.routing.ComponentProvider; | ||
import net.cactusthorn.routing.RoutingConfig; | ||
import net.cactusthorn.routing.RoutingInitializationException; | ||
|
||
public class FormParameterTest extends InvokeTestAncestor { | ||
|
||
public static class EntryPoint1 { | ||
|
||
@POST public void wrongConsumes(Form form) { | ||
} | ||
|
||
@POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) // | ||
public void form(Form form) { | ||
} | ||
} | ||
|
||
public static class EntryPoint1Provider implements ComponentProvider { | ||
@Override // | ||
public Object provide(Class<?> clazz, HttpServletRequest request) { | ||
return new EntryPoint1(); | ||
} | ||
} | ||
|
||
private static final RoutingConfig CONFIG = RoutingConfig.builder(new EntryPoint1Provider()).addResource(EntryPoint1.class).build(); | ||
|
||
@Test public void wrongContentType() { | ||
assertThrows(RoutingInitializationException.class, () -> parameterInfo(EntryPoint1.class, "wrongConsumes", CONFIG)); | ||
} | ||
|
||
@Test public void form() throws Exception { | ||
MethodParameter mp = parameterInfo(EntryPoint1.class, "form", CONFIG); | ||
|
||
Map<String, String[]> parameters = new HashMap<>(); | ||
parameters.put("aaaa", new String[] { "super value" }); | ||
|
||
Mockito.when(request.getParameterNames()).thenReturn(Collections.enumeration(parameters.keySet())); | ||
Mockito.when(request.getParameterValues("aaaa")).thenReturn(parameters.get("aaaa")); | ||
|
||
Form f = (Form) mp.findValue(request, null, null, null); | ||
assertEquals("super value", f.asMap().getFirst("aaaa")); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
...src/main/java/net/cactusthorn/routing/demo/jetty/UnsupportedOperationExceptionMapper.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,11 @@ | ||
package net.cactusthorn.routing.demo.jetty; | ||
|
||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
|
||
public class UnsupportedOperationExceptionMapper implements ExceptionMapper<UnsupportedOperationException> { | ||
|
||
@Override public Response toResponse(UnsupportedOperationException exception) { | ||
return Response.status(Response.Status.SERVICE_UNAVAILABLE).build(); | ||
} | ||
} |
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