Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambiguous methods bridge #646

Merged
merged 4 commits into from Nov 28, 2018
Merged
Changes from 3 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+78 −14
Diff settings

Always

Just for now

@@ -47,6 +47,11 @@
</developer>
</developers>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-auth-roles</artifactId>
@@ -135,19 +135,17 @@ public AbstractRestResource(T serialDeserial, IRoleCheckingStrategy roleChecking
this.roleCheckingStrategy = roleCheckingStrategy;
this.mappedMethods = loadAnnotatedMethods();
this.mappedMethodsInfo = loadAnnotatedMethodsInfo();
this.bundleResolver = new DefaultBundleResolver(loadBoundleClasses());
this.bundleResolver = new DefaultBundleResolver(loadBundleClasses());
}

/**
* Build a list of classes to use to search for a valid bundle. This list is
* made of the classes of the validators registered with abstractResource
* and of the class of the abstractResource.
*
* @param abstractResource
* the abstract REST resource that is using the validator
* @return the list of the classes to use.
*/
private List<Class<?>> loadBoundleClasses()
private List<Class<?>> loadBundleClasses()
{
Collection<IValidator<?>> validators = declaredValidators.values();
List<Class<?>> validatorsClasses = ReflectionUtils.getElementsClasses(validators);
@@ -398,7 +396,7 @@ protected void onAfterMethodInvoked(MethodMappingInfo mappedMethod, Attributes a
* The current response object.
* @param result
* The object to write to response.
* @param restMimeFormats
* @param mimeType
* The MIME type to use to serialize data
*/
public void objectToResponse(Object result, WebResponse response, String mimeType)
@@ -472,7 +470,7 @@ private ScoreMethodAndExtractPathVars selectMostSuitedMethod(AttributesWrapper a
{
// if we have more than one method with the highest score, throw
// ambiguous exception.
throwAmbiguousMethodsException(scoredMethods.getFirstValue(highestScore));
throwAmbiguousMethodsException(scoredMethods.get(highestScore));
}

return scoredMethods.getFirstValue(highestScore);
@@ -482,10 +480,9 @@ private ScoreMethodAndExtractPathVars selectMostSuitedMethod(AttributesWrapper a
* Throw an exception if two o more methods have the same "score" for the current request. See
* method selectMostSuitedMethod.
*
* @param list
* the list of ambiguous methods.
* @param methods the list of ambiguous methods.
*/
private void throwAmbiguousMethodsException(ScoreMethodAndExtractPathVars... methods)
private void throwAmbiguousMethodsException(List<ScoreMethodAndExtractPathVars> methods)
{
WebRequest request = getCurrentWebRequest();
String methodsNames = "";
@@ -496,7 +493,9 @@ private void throwAmbiguousMethodsException(ScoreMethodAndExtractPathVars... met
methodsNames += ", ";

MethodMappingInfo urlMappingInfo = method.getMethodInfo();
methodsNames += urlMappingInfo.getMethod().getName();
methodsNames += urlMappingInfo.getMethod().getReturnType().getSimpleName() + " " +
This conversation was marked as resolved by froque

This comment has been minimized.

Copy link
@solomax

solomax Nov 28, 2018

Contributor

Maybe it worth to use StringBuilder in this method?

This comment has been minimized.

Copy link
@froque

froque Nov 28, 2018

Author Contributor

changed

urlMappingInfo.getMethod().getDeclaringClass().getSimpleName() + "." +
urlMappingInfo.getMethod().getName();
}

throw new WicketRuntimeException("Ambiguous methods mapped for the current request: URL '" +
@@ -535,7 +534,7 @@ protected void onInitialize(T objSerialDeserial)

isUsingAuthAnnot = isUsingAuthAnnot || authorizeInvocation != null;

if (methodMapped != null)
if (methodMapped != null && !method.isBridge())
{
HttpMethod httpMethod = methodMapped.httpMethod();
MethodMappingInfo methodMappingInfo = new MethodMappingInfo(methodMapped, method);
@@ -545,9 +544,9 @@ protected void onInitialize(T objSerialDeserial)
throw new WicketRuntimeException(
"Mapped methods use a MIME type not supported by obj serializer/deserializer!");

mappedMethods.addValue(
methodMappingInfo.getSegmentsCount() + "_" + httpMethod.getMethod(),
methodMappingInfo);
String key = methodMappingInfo.getSegmentsCount() + "_" + httpMethod.getMethod();
mappedMethods.addValue(key, methodMappingInfo);
log.debug("Added mapped method: " + method + " with key: " + key);
This conversation was marked as resolved by froque

This comment has been minimized.

Copy link
@solomax

solomax Nov 28, 2018

Contributor

I would rewrite
log.debug("Added mapped method: " + method + " with key: " + key);
as
log.debug("Added mapped method: {} with key: {}", method, key);
should be more effective

This comment has been minimized.

Copy link
@froque

froque Nov 28, 2018

Author Contributor

changed

}
}
// if AuthorizeInvocation has been found but no role-checker has been
@@ -0,0 +1,5 @@
package org.wicketstuff.rest;

public class Developer extends Person {

}
@@ -270,6 +270,19 @@ public void testMultiFormat() throws Exception
assertEquals(writer.toString(), tester.getLastResponseAsString());
}

@Test
public void testExtendsMethod() throws Exception
{
// test JSON response
tester.getRequest().setMethod("POST");
tester.executeUrl("./api4");

JSONObject actual = new JSONObject(tester.getLastResponseAsString());
Assert.assertEquals("Smith", actual.getString("surname"));
Assert.assertEquals("Mary", actual.getString("name"));
Assert.assertEquals("m.smith@gmail.com", actual.getString("email"));
}

protected void testIfResponseStringIsEqual(String value)
{
Assert.assertEquals(value, tester.getLastResponseAsString());
@@ -31,6 +31,7 @@
import org.wicketstuff.rest.contenthandling.webserialdeserial.JsonTestWebSerialDeserial;
import org.wicketstuff.rest.contenthandling.webserialdeserial.MultiFormatSerialDeserial;
import org.wicketstuff.rest.contenthandling.webserialdeserial.XmlTestWebSerialDeserial;
import org.wicketstuff.rest.resource.ExtendsRestResource;
import org.wicketstuff.rest.resource.MultiFormatRestResource;
import org.wicketstuff.rest.resource.RegExpRestResource;
import org.wicketstuff.rest.resource.RestResourceFullAnnotated;
@@ -110,6 +111,18 @@ public IResource getResource()
}

});

mountResource("/api4", new ResourceReference("restReference")
{

@Override
public IResource getResource()
{
return new ExtendsRestResource(
new JsonTestWebSerialDeserial(), WicketApplication.this);
}

});
}

@Override
@@ -0,0 +1,28 @@
package org.wicketstuff.rest.resource;

import org.apache.wicket.authroles.authorization.strategies.role.IRoleCheckingStrategy;
import org.wicketstuff.rest.Developer;
import org.wicketstuff.rest.Person;
import org.wicketstuff.rest.annotations.MethodMapping;
import org.wicketstuff.rest.annotations.parameters.RequestBody;
import org.wicketstuff.rest.contenthandling.webserialdeserial.TextualWebSerialDeserial;
import org.wicketstuff.restutils.http.HttpMethod;

public class ExtendsRestResource extends RestResourceFullAnnotated {

public ExtendsRestResource(TextualWebSerialDeserial jsonSerialDeserial,
IRoleCheckingStrategy roleCheckingStrategy)
{
super(jsonSerialDeserial, roleCheckingStrategy);
}

@MethodMapping(value = "/", httpMethod = HttpMethod.POST)
public Developer testMethodPost()
{
Developer d = new Developer();
d.setName("Mary");
d.setSurname("Smith");
d.setEmail("m.smith@gmail.com");
return d;
}
}
@@ -0,0 +1 @@
org.slf4j.simpleLogger.defaultLogLevel=debug
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.