| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* The contents of this file are subject to the terms of either the GNU |
| 7 |
* General Public License Version 2 only ("GPL") or the Common Development |
| 8 |
* and Distribution License("CDDL") (collectively, the "License"). You |
| 9 |
* may not use this file except in compliance with the License. You can |
| 10 |
* obtain a copy of the License at |
| 11 |
* http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 |
* or packager/legal/LICENSE.txt. See the License for the specific |
| 13 |
* language governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* When distributing the software, include this License Header Notice in each |
| 16 |
* file and include the License file at packager/legal/LICENSE.txt. |
| 17 |
* |
| 18 |
* GPL Classpath Exception: |
| 19 |
* Oracle designates this particular file as subject to the "Classpath" |
| 20 |
* exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 |
* file that accompanied this code. |
| 22 |
* |
| 23 |
* Modifications: |
| 24 |
* If applicable, add the following below the License Header, with the fields |
| 25 |
* enclosed by brackets [] replaced by your own identifying information: |
| 26 |
* "Portions Copyright [year] [name of copyright owner]" |
| 27 |
* |
| 28 |
* Contributor(s): |
| 29 |
* If you wish your version of this file to be governed by only the CDDL or |
| 30 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 |
* elects to include this software in this distribution under the [CDDL or GPL |
| 32 |
* Version 2] license." If you don't indicate a single choice of license, a |
| 33 |
* recipient has the option to distribute your version of this file under |
| 34 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 |
* its licensees as provided above. However, if you add GPL Version 2 code |
| 36 |
* and therefore, elected the GPL Version 2 license, then the option applies |
| 37 |
* only if the new code is made subject to such option by the copyright |
| 38 |
* holder. |
| 39 |
*/ |
| 40 |
|
| 41 |
package com.sun.jersey.impl.client; |
| 42 |
|
| 43 |
import com.sun.jersey.core.spi.component.ComponentContext; |
| 44 |
import com.sun.jersey.core.spi.component.ComponentScope; |
| 45 |
import com.sun.jersey.impl.container.grizzly2.*; |
| 46 |
import com.sun.jersey.api.client.Client; |
| 47 |
import com.sun.jersey.api.client.WebResource; |
| 48 |
import com.sun.jersey.api.client.config.ClientConfig; |
| 49 |
import com.sun.jersey.api.client.config.DefaultClientConfig; |
| 50 |
import com.sun.jersey.spi.inject.Injectable; |
| 51 |
import com.sun.jersey.spi.inject.InjectableProvider; |
| 52 |
import java.io.IOException; |
| 53 |
import java.io.OutputStream; |
| 54 |
import java.lang.annotation.Annotation; |
| 55 |
import java.lang.reflect.Type; |
| 56 |
import javax.ws.rs.POST; |
| 57 |
import javax.ws.rs.Path; |
| 58 |
import javax.ws.rs.WebApplicationException; |
| 59 |
import javax.ws.rs.core.Context; |
| 60 |
import javax.ws.rs.core.MediaType; |
| 61 |
import javax.ws.rs.core.MultivaluedMap; |
| 62 |
import javax.ws.rs.ext.MessageBodyWriter; |
| 63 |
import javax.ws.rs.ext.Provider; |
| 64 |
|
| 65 |
/** |
| 66 |
* |
| 67 |
* @author [email protected] |
| 68 |
*/ |
| 69 |
public class InjectableProviderTest extends AbstractGrizzlyServerTester { |
| 70 |
public static class InjectableType { |
| 71 |
String s; |
| 72 |
|
| 73 |
InjectableType(String s) { |
| 74 |
this.s = s; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Provider |
| 79 |
public static class InjectableTypeProvider implements InjectableProvider<Context, Type> { |
| 80 |
|
| 81 |
public ComponentScope getScope() { |
| 82 |
return ComponentScope.Singleton; |
| 83 |
} |
| 84 |
|
| 85 |
public Injectable getInjectable(ComponentContext ic, Context a, Type c) { |
| 86 |
if (c != InjectableType.class) |
| 87 |
return null; |
| 88 |
final InjectableType it = new InjectableType("INJECT"); |
| 89 |
|
| 90 |
return new Injectable<InjectableType>() { |
| 91 |
public InjectableType getValue() { |
| 92 |
return it; |
| 93 |
} |
| 94 |
}; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public static class StringType { |
| 99 |
String s; |
| 100 |
|
| 101 |
StringType(String s) { |
| 102 |
this.s = s; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Provider |
| 107 |
public static class StringTypeWriter implements MessageBodyWriter<StringType> { |
| 108 |
@Context InjectableType it; |
| 109 |
|
| 110 |
public boolean isWriteable(Class<?> type, Type genericType, |
| 111 |
Annotation[] annotations, MediaType mediaType) { |
| 112 |
return StringType.class.isAssignableFrom(type); |
| 113 |
} |
| 114 |
|
| 115 |
public long getSize(StringType t, Class<?> type, Type genericType, |
| 116 |
Annotation[] annotations, MediaType mediaType) { |
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
public void writeTo(StringType t, Class<?> type, Type genericType, |
| 121 |
Annotation[] annotations, MediaType mediaType, |
| 122 |
MultivaluedMap<String, Object> httpHeaders, |
| 123 |
OutputStream entityStream) throws IOException, WebApplicationException { |
| 124 |
String s = it.s + " " + t.s; |
| 125 |
entityStream.write(s.getBytes()); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Path("/") |
| 130 |
public static class Resource { |
| 131 |
@POST |
| 132 |
public String get(String s) { |
| 133 |
return s; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
public InjectableProviderTest(String testName) { |
| 138 |
super(testName); |
| 139 |
} |
| 140 |
|
| 141 |
public void testInjectableProvider() throws Exception { |
| 142 |
startServer(Resource.class); |
| 143 |
|
| 144 |
ClientConfig cc = new DefaultClientConfig(); |
| 145 |
cc.getClasses().add(InjectableTypeProvider.class); |
| 146 |
cc.getClasses().add(StringTypeWriter.class); |
| 147 |
Client c = Client.create(cc); |
| 148 |
|
| 149 |
WebResource r = c.resource(getUri().build()); |
| 150 |
String s = r.post(String.class, new StringType("test")); |
| 151 |
assertEquals("INJECT test", s); |
| 152 |
} |
| 153 |
} |