Skip to content

Commit

Permalink
JERSEY-2928: Added support for @BeanParam beans.
Browse files Browse the repository at this point in the history
Handling includes bean setter methods with @QueryParam annotations and
fields with @QueryParam annotations.

Fixed one Checkstyle violation

Satisfy the copyright.year.wrong check

Change-Id: I82d5e76495d86e98506f0825c3cfcab496d18d3b
  • Loading branch information
sfuhrm authored and stepanv committed Nov 27, 2015
1 parent 7d50c86 commit e570863
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -42,6 +42,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -52,6 +53,7 @@
import javax.ws.rs.HttpMethod;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.BeanParam;
import javax.ws.rs.core.Link;

import org.glassfish.jersey.linking.mapping.ResourceMappingContext;
Expand Down Expand Up @@ -165,20 +167,11 @@ public static String getLinkTemplate(ResourceMappingContext rmc, InjectLink link
builder.append(methodTemplate);
}

// append query parameters
StringBuilder querySubString = new StringBuilder();
for (Annotation paramAnns[] : method.getParameterAnnotations()) {
for (Annotation ann : paramAnns) {
if (ann.annotationType() == QueryParam.class) {
querySubString.append(((QueryParam) ann).value());
querySubString.append(',');
}
}
}
CharSequence querySubString = extractQueryParams(method);

if (querySubString.length() > 0) {
builder.append("{?");
builder.append(querySubString.subSequence(0, querySubString.length() - 1));
builder.append(querySubString);
builder.append("}");
}

Expand All @@ -193,6 +186,47 @@ public static String getLinkTemplate(ResourceMappingContext rmc, InjectLink link
return template;
}

private static CharSequence extractQueryParams(AnnotatedMethod method) throws SecurityException {
// append query parameters
StringBuilder querySubString = new StringBuilder();
int parameterIndex = 0;
for (Annotation paramAnns[] : method.getParameterAnnotations()) {
for (Annotation ann : paramAnns) {
if (ann.annotationType() == QueryParam.class) {
querySubString.append(((QueryParam) ann).value());
querySubString.append(',');
}
if (ann.annotationType() == BeanParam.class) {
Class<?> beanParamType = method.getParameterTypes()[parameterIndex];
Field fields[] = beanParamType.getFields();
for (Field field : fields) {
QueryParam queryParam = field.getAnnotation(QueryParam.class);
if (queryParam != null) {
querySubString.append(queryParam.value());
querySubString.append(',');
}
}
Method beanMethods[] = beanParamType.getMethods();
for (Method beanMethod : beanMethods) {
QueryParam queryParam = beanMethod.getAnnotation(QueryParam.class);
if (queryParam != null) {
querySubString.append(queryParam.value());
querySubString.append(',');
}
}
}
}
parameterIndex++;
}

CharSequence result = "";

if (querySubString.length() > 0) {
result = querySubString.subSequence(0, querySubString.length() - 1);
}
return result;
}

/**
* TODO javadoc.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.logging.Logger;
import java.util.regex.MatchResult;
import java.util.zip.ZipEntry;
import javax.ws.rs.BeanParam;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -688,6 +689,59 @@ public void testQueryResourceWithoutBindings() {
mockUriInfo.getQueryParameters().clear();
}

/** Bean param with method setter QueryParam. */
public static class BeanParamBeanA {
private String qparam;
@QueryParam("qparam")
public void setQParam(String qparam) {
this.qparam = qparam;
}
}

/** Bean param with field QueryParam. */
public static class BeanParamBeanB {
@QueryParam("query") public String query;
}

@Path("a")
public static class BeanParamQueryResource {

@Path("b")
@GET
public String getB(@BeanParam BeanParamBeanA beanParamBeanA, @BeanParam BeanParamBeanB beanParamBeanB) {
return "hello world";
}
}

public static class BeanParamResourceBean {

public String getQueryParam() {
return queryExample;
}

private String queryExample;

public BeanParamResourceBean(String queryExample) {
this.queryExample = queryExample;
}

@InjectLink(resource = BeanParamQueryResource.class, method = "getB",
bindings = {
@Binding(name = "query", value = "${instance.queryParam}"),
@Binding(name = "qparam", value = "foo")
})
public String uri;
}

@Test
public void testBeanParamResource() {
LOG.info("BeanParamResource");
FieldProcessor<BeanParamResourceBean> instance = new FieldProcessor(BeanParamResourceBean.class);
BeanParamResourceBean testClass = new BeanParamResourceBean("queryExample");
instance.processLinks(testClass, mockUriInfo, mockRmc);
assertEquals("/application/resources/a/b?qparam=foo&query=queryExample", testClass.uri);
}

public static class TestClassK {

public static final ZipEntry zipEntry = new ZipEntry("entry");
Expand Down

0 comments on commit e570863

Please sign in to comment.