This repository was archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathConsumerFactory.java
More file actions
181 lines (161 loc) · 6.84 KB
/
ConsumerFactory.java
File metadata and controls
181 lines (161 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
* Copyright (c) 2012,2014 EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Holger Staudacher - initial API and implementation
******************************************************************************/
package com.eclipsesource.jaxrs.consumer;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URL;
import javax.ws.rs.Path;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.ext.Provider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import com.eclipsesource.jaxrs.consumer.internal.ClientHelper;
import com.eclipsesource.jaxrs.consumer.internal.ResourceInvocationHandler;
/**
* <p>
* A static factory for creating consumer objects out of <code>@Path</code> annotated interfaces.
* </p>
*
* @see Path
* @see Provider
*/
public class ConsumerFactory {
/**
* <p>
* Creates a consumer object out of a <code>@Path</code> interface. It will create a proxy object of the interface
* that calls the specified service using the passed in base url.
* </p>
*
* @param baseUrl The server url hosting the specified service.
* @param type The <code>@Path</code> annotated interface class object.
* @return a proxy object for the passed in type.
*/
public static <T> T createConsumer( String baseUrl, Class<T> type ) {
return createConsumer( baseUrl, new ClientConfig(), type );
}
/**
* <p>
* Creates a consumer object out of a <code>@Path</code> interface. It will create a proxy object of the interface
* that calls the specified service using the passed in base url. The de/serialization is done using the passed in
* <code>@Provider</code> objects. The passed in {@link Configuration} will be used to create the {@link Client}
* instances.
* </p>
*
* @param baseUrl The server url hosting the specified service.
* @param configuration The {@link Configuration} to use for building the {@link Client}.
* @param type The <code>@Path</code> annotated interface class object.
* @param customProvider An array of <code>@Provider</code> object for de/serialization.
* @return a proxy object for the passed in type.
*/
@SuppressWarnings( "unchecked" )
public static <T> T createConsumer( String baseUrl, Configuration configuration, Class<T> type ) {
checkUrl( baseUrl );
checkType( type );
checkConfiguration( configuration );
checkAnnotation( type );
ensureTypeIsAnInterface( type );
Path path = type.getAnnotation( Path.class );
ensureMultiPartFeature( configuration, type );
return ( T )Proxy.newProxyInstance( type.getClassLoader(),
new Class<?>[] { type },
createHandler( baseUrl, configuration, path ) );
}
/**
* <p>
* Creates a consumer object out of a <code>@Path</code> interface. It will create a proxy object of the interface
* that calls the specified service using the passed in base url. The de/serialization is done using the passed in
* <code>@Provider</code> objects. The passed in {@link Client} will be used to send requests.
* </p>
*
* @param baseUrl The server url hosting the specified service.
* @param client The {@link Client} to use for sending requests
* @param type The <code>@Path</code> annotated interface class object.
* @param customProvider An array of <code>@Provider</code> object for de/serialization.
* @return a proxy object for the passed in type.
*/
@SuppressWarnings( "unchecked" )
public static <T> T createConsumer( String baseUrl, Client client, Class<T> type ) {
checkUrl( baseUrl );
checkType( type );
checkClient( client );
checkAnnotation( type );
ensureTypeIsAnInterface( type );
Path path = type.getAnnotation( Path.class );
ensureMultiPartFeature( client.getConfiguration(), type );
return ( T )Proxy.newProxyInstance( type.getClassLoader(),
new Class<?>[] { type },
createHandler( baseUrl, client, path ) );
}
private static <T> void ensureMultiPartFeature( Configuration configuration, Class<T> type ) {
if( hasFormDataParam( type ) ) {
( ( ClientConfig )configuration ).register( MultiPartFeature.class );
}
}
private static boolean hasFormDataParam( Class<?> type ) {
for( Method method : type.getMethods() ) {
if( ClientHelper.hasFormAnnotation( method, FormDataParam.class ) ) {
return true;
}
}
return false;
}
private static ResourceInvocationHandler createHandler( String baseUrl,
Configuration configuration,
Path path )
{
return new ResourceInvocationHandler( baseUrl + path.value(), configuration );
}
private static ResourceInvocationHandler createHandler( String baseUrl,
Client client,
Path path )
{
return new ResourceInvocationHandler( baseUrl + path.value(), client );
}
private static void checkUrl( String url ) {
try {
new URL( url );
} catch( MalformedURLException invalidUrlException ) {
throw new IllegalArgumentException( url + " is not a valid url", invalidUrlException );
}
}
private static void checkType( Class<?> type ) {
if( type == null ) {
throw new IllegalArgumentException( "type must not be null." );
}
}
private static void checkConfiguration(Configuration configuration) {
if( configuration == null ) {
throw new IllegalArgumentException( "Configuration must not be null" );
}
}
private static void checkClient(Client client) {
if( client == null ) {
throw new IllegalArgumentException( "Client must not be null" );
}
}
private static void checkAnnotation( Class<?> type ) {
if( !type.isAnnotationPresent( Path.class ) ) {
throw new IllegalArgumentException( type.getName() + " is not a Resource. No @Path Annotation." );
}
}
private static void ensureTypeIsAnInterface( Class<?> type ) {
if( !type.isInterface() ) {
throw new IllegalArgumentException( type.getName() + " is not an interface. You do not " +
"want a dependency to cglib, do you?" );
}
}
private ConsumerFactory() {
// prevent instantiation
}
}