Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed Apr 8, 2014
2 parents 5e758c9 + 65d2bea commit 4f4172e
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 16 deletions.
@@ -1,5 +1,26 @@
package org.jboss.resteasy.client.jaxrs.internal.proxy.processors;

import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.Stack;

import javax.ws.rs.BeanParam;
import javax.ws.rs.CookieParam;
import javax.ws.rs.Encoded;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.annotations.Form;
import org.jboss.resteasy.client.ClientURI;
import org.jboss.resteasy.client.jaxrs.internal.ClientConfiguration;
Expand All @@ -14,22 +35,6 @@
import org.jboss.resteasy.util.FindAnnotation;
import org.jboss.resteasy.util.MediaTypeHelper;

import javax.ws.rs.BeanParam;
import javax.ws.rs.CookieParam;
import javax.ws.rs.Encoded;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.MediaType;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class ProcessorFactory
{

Expand All @@ -46,6 +51,9 @@ public static Object[] createProcessors(Class declaringClass, Method method, Cli
Class<?> type = method.getParameterTypes()[i];
Annotation[] annotations = method.getParameterAnnotations()[i];
Type genericType = method.getGenericParameterTypes()[i];
if (TypeVariable.class.isInstance(genericType) && declaringClass.isInterface() && !declaringClass.equals(method.getDeclaringClass())) {
genericType = getTypeArgument((TypeVariable)genericType, declaringClass, method.getDeclaringClass());
}
AccessibleObject target = method;
params[i] = ProcessorFactory.createProcessor(declaringClass, configuration, type, annotations, genericType, target, defaultConsumes, false);
}
Expand Down Expand Up @@ -146,4 +154,56 @@ else if (!ignoreBody)
}
return processor;
}

static Type getTypeArgument(TypeVariable<?> var, Class<?> clazz, Class<?> baseInterface) {
TypeVariable<?> tv = var;
// collect superinterfaces
Stack<Type> superinterfaces = new Stack<Type>();
Type currentType;
Class<?> currentClass = clazz;
recursivePush(currentClass, baseInterface, superinterfaces);

while (!superinterfaces.isEmpty()) {
currentType = superinterfaces.pop();

if (currentType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) currentType;
Class<?> rawType = (Class) pt.getRawType();
int argIndex = Arrays.asList(rawType.getTypeParameters()).indexOf(tv);
if (argIndex > -1) {
Type typeArg = pt.getActualTypeArguments()[argIndex];
if (typeArg instanceof TypeVariable) {
// type argument is another type variable - look for the value of that
// variable in subclasses
tv = (TypeVariable<?>) typeArg;
continue;
} else {
// found the value - return it
return typeArg;
}
}
}

// needed type argument not supplied - break and throw exception
break;
}
throw new IllegalArgumentException(var + " does not specify the type parameter T of GenericType<T>");
}

static void recursivePush(Type t, Class<?> baseInterface, Stack<Type> superinterfaces) {
Class<?> currentClass = null;
if (t instanceof Class) {
currentClass = (Class) t;
} else if (t instanceof ParameterizedType) {
currentClass = (Class) ((ParameterizedType) t).getRawType();
}
if (baseInterface.isAssignableFrom(currentClass)) {
superinterfaces.push(t);

for (Type otherType : currentClass.getGenericInterfaces()) {
recursivePush(otherType, baseInterface, superinterfaces);
}
}

}
}
114 changes: 114 additions & 0 deletions jaxrs/war-tests/client-typevar-test/pom.xml
@@ -0,0 +1,114 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs-all</artifactId>
<version>3.0.8.Final</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.jboss.resteasy.test</groupId>
<artifactId>test-client-typevar-war</artifactId>
<packaging>war</packaging>
<name>client-typevar-test</name>
<description/>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<finalName>encoding-test</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.15</version>
<configuration>
<!-- By default the artifactId is taken, override it with something simple -->
<contextPath>/</contextPath>
<scanIntervalSeconds>2</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<connectors>
<connector
implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9095</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,16 @@
package org.jboss.resteasy.tests.typevar.sample;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;


public interface Hello<T> {

@POST
@Path("/hello")
@Produces("text/plain")
@Consumes("text/plain")
String sayHi(T in);
}
@@ -0,0 +1,8 @@
package org.jboss.resteasy.tests.typevar.sample;

import javax.ws.rs.Path;

@Path(value = "/say")
public interface HelloString extends Hello<String> {

}
@@ -0,0 +1,11 @@
package org.jboss.resteasy.tests.typevar.sample;

public class HelloStringImpl implements HelloString {

@Override
public String sayHi(String in) {

return in;
}

}
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Archetype RestEasy Web Application</display-name>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>org.jboss.resteasy.tests.typevar.sample.HelloStringImpl</param-value>
</context-param>

<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions jaxrs/war-tests/client-typevar-test/src/main/webapp/index.jsp
@@ -0,0 +1,8 @@
<html>

<title>Welcome RestEasy </title>
<link rel="stylesheet" href="jboss.css" type="text/css"/>
<body>
<h1>Welcome to RestEasy</h1>
</body>
</html>
38 changes: 38 additions & 0 deletions jaxrs/war-tests/client-typevar-test/src/main/webapp/jboss.css
@@ -0,0 +1,38 @@
/*
Document : jboss
Created on : Feb 25, 2009, 1:57:15 AM
Author : edgarsilva
Description:
Purpose of the stylesheet follows.
*/

/*
TODO customize this sample style
Syntax recommendation http://www.w3.org/TR/REC-CSS2/
*/

root {
display: block;

}

h1, h2, h3, h4, h5, h6 {
color: #4a5d75;
line-height: 130%;
margin-top: 0em;
font-family: 'Lucida Grande', Geneva, Verdana, Arial, sans-serif;
background-color: transparent;
}

h1 {
/* background: #3B4E64 none repeat scroll 0% 50%; */
background-image: url(bg.png);
background-repeat: no-repeat;
border-top: 1px dotted #CCCCCC;
line-height: 1.2em;
color: #182737;
font-size: 2em;
padding: 1.5em;
}


@@ -0,0 +1,23 @@
package org.jboss.resteasy.tests.typevar.sample.test;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.tests.typevar.sample.HelloString;
import org.junit.Assert;
import org.junit.Test;

public class SayHelloTest {

@Test
public void testEcho()
{
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost:9095");
HelloString proxy = target.proxy(HelloString.class);
String hello = proxy.sayHi("hello");
Assert.assertEquals("hello", hello);
}


}
1 change: 1 addition & 0 deletions jaxrs/war-tests/pom.xml
Expand Up @@ -33,5 +33,6 @@
<module>oauth-servlet-test</module>
<!-- <module>jsapi-servlet-test</module> -->
<module>application-test</module>
<module>client-typevar-test</module>
</modules>
</project>

0 comments on commit 4f4172e

Please sign in to comment.