66import com .flickr4java .flickr .uploader .UploadMetaData ;
77import com .flickr4java .flickr .util .DebugInputStream ;
88import com .flickr4java .flickr .util .IOUtilities ;
9+ import com .flickr4java .flickr .util .OAuthUtilities ;
910import com .flickr4java .flickr .util .UrlUtilities ;
10- import com .github .scribejava .apis .FlickrApi ;
11- import com .github .scribejava .core .builder .ServiceBuilder ;
12- import com .github .scribejava .core .httpclient .jdk .JDKHttpClientConfig ;
1311import com .github .scribejava .core .model .OAuth1AccessToken ;
1412import com .github .scribejava .core .model .OAuthRequest ;
1513import com .github .scribejava .core .model .Verb ;
3129import java .util .Base64 ;
3230import java .util .HashMap ;
3331import java .util .Map ;
34- import java .util .UUID ;
3532import java .util .concurrent .ExecutionException ;
3633
3734/**
@@ -144,7 +141,7 @@ public com.flickr4java.flickr.Response get(String path, Map<String, Object> para
144141
145142 RequestContext requestContext = RequestContext .getRequestContext ();
146143 Auth auth = requestContext .getAuth ();
147- OAuth10aService service = createOAuthService (apiKey , sharedSecret );
144+ OAuth10aService service = OAuthUtilities . createOAuthService (apiKey , sharedSecret , connectTimeoutMs , readTimeoutMs );
148145 if (auth != null ) {
149146 OAuth1AccessToken requestToken = new OAuth1AccessToken (auth .getToken (), auth .getTokenSecret ());
150147 service .signRequest (requestToken , request );
@@ -162,7 +159,7 @@ public com.flickr4java.flickr.Response get(String path, Map<String, Object> para
162159
163160 try {
164161 return handleResponse (request , service );
165- } catch (IllegalAccessException | InstantiationException | SAXException | IOException | InterruptedException | ExecutionException | ParserConfigurationException e ) {
162+ } catch (ReflectiveOperationException | SAXException | IOException | InterruptedException | ExecutionException | ParserConfigurationException e ) {
166163 throw new FlickrRuntimeException (e );
167164 }
168165 }
@@ -177,15 +174,13 @@ public com.flickr4java.flickr.Response get(String path, Map<String, Object> para
177174 @ Override
178175 public com .flickr4java .flickr .Response post (String path , Map <String , Object > parameters , String apiKey , String sharedSecret ) throws FlickrException {
179176
180- OAuthRequest request = new OAuthRequest (Verb .POST , buildUrl (path ));
181-
182- buildNormalPostRequest (parameters , request );
177+ OAuthRequest request = OAuthUtilities .buildNormalPostRequest (parameters , buildUrl (path ));
183178
184179 OAuth10aService service = createAndSignRequest (apiKey , sharedSecret , request );
185180
186181 try {
187182 return handleResponse (request , service );
188- } catch (IllegalAccessException | InterruptedException | ExecutionException | InstantiationException | IOException | SAXException | ParserConfigurationException e ) {
183+ } catch (ReflectiveOperationException | InterruptedException | ExecutionException | IOException | SAXException | ParserConfigurationException e ) {
189184 throw new FlickrRuntimeException (e );
190185 }
191186 }
@@ -201,58 +196,41 @@ public com.flickr4java.flickr.Response post(String path, Map<String, Object> par
201196 @ Override
202197 public com .flickr4java .flickr .Response postMultiPart (String path , UploadMetaData metaData , Payload payload , String apiKey , String sharedSecret ) throws FlickrException {
203198
204- OAuthRequest request = new OAuthRequest (Verb .POST , buildUrl (path ));
205199 Map <String , String > uploadParameters = new HashMap <>(metaData .getUploadParameters ());
206-
207- buildMultipartRequest (uploadParameters , request );
200+ OAuthRequest request = OAuthUtilities .buildMultipartRequest (uploadParameters , buildUrl (path ));
208201
209202 OAuth10aService service = createAndSignRequest (apiKey , sharedSecret , request );
210203
211204 // Ensure all parameters (including oauth) are added to payload so signature matches
212205 uploadParameters .putAll (request .getOauthParameters ());
213206
214- request .addMultipartPayload ( String . format ( "form-data; name= \" photo \" ; filename= \" %s \" " , metaData . getFilename ()), metaData .getFilemimetype (), payload .getPayload ());
207+ request .addFileByteArrayBodyPartPayloadInMultipartPayload ( metaData .getFilemimetype (), payload .getPayload ());
215208 uploadParameters .entrySet ().forEach (e ->
216- request .addMultipartPayload ( String . format ( "form-data; name= \" %s \" " , e . getKey ()), null , e .getValue ().getBytes ()));
209+ request .addFileByteArrayBodyPartPayloadInMultipartPayload ( null , e .getValue ().getBytes ()));
217210
218211 try {
219212 return handleResponse (request , service );
220- } catch (IllegalAccessException | InterruptedException | ExecutionException | InstantiationException | IOException | SAXException | ParserConfigurationException e ) {
213+ } catch (ReflectiveOperationException | InterruptedException | ExecutionException | IOException | SAXException | ParserConfigurationException e ) {
221214 throw new FlickrRuntimeException (e );
222215 }
223216 }
224217
225218 private OAuth10aService createAndSignRequest (String apiKey , String sharedSecret , OAuthRequest request ) {
226- RequestContext requestContext = RequestContext .getRequestContext ();
227- Auth auth = requestContext .getAuth ();
228- OAuth10aService service = createOAuthService (apiKey , sharedSecret );
229- if (auth != null ) {
230- OAuth1AccessToken requestToken = new OAuth1AccessToken (auth .getToken (), auth .getTokenSecret ());
231- service .signRequest (requestToken , request );
232- }
233-
234- if (proxyAuth ) {
235- request .addHeader ("Proxy-Authorization" , "Basic " + getProxyCredentials ());
236- }
237-
238- if (Flickr .debugRequest ) {
239- logger .debug ("POST: " + request .getCompleteUrl ());
240- }
241-
242- return service ;
219+ OAuth10aService service = OAuthUtilities .createOAuthService (apiKey , sharedSecret , connectTimeoutMs , readTimeoutMs );
220+ OAuthUtilities .signRequest (service , request , proxyAuth ? getProxyCredentials () : null );
221+ return service ;
243222 }
244223
245224 private String buildUrl (String path ) {
246225 return String .format ("%s://%s%s" , getScheme (), getHost (), path );
247226 }
248227
249- private Response handleResponse (OAuthRequest request , OAuth10aService service ) throws InterruptedException , ExecutionException , IOException , SAXException , InstantiationException , IllegalAccessException , ParserConfigurationException , FlickrException {
228+ private Response handleResponse (OAuthRequest request , OAuth10aService service ) throws InterruptedException , ExecutionException , IOException , SAXException , ParserConfigurationException , FlickrException , ReflectiveOperationException {
250229 com .github .scribejava .core .model .Response scribeResponse = service .execute (request );
251230
252231 if (!scribeResponse .isSuccessful ()) {
253232 throw new FlickrException (FLICKR_SERVICE_UNAVAILABLE , String .format ("Received '%s' error from Flickr with status %d" , scribeResponse .getMessage (), scribeResponse .getCode ()));
254233 }
255- Response f4jResponse ;
256234 String strXml = scribeResponse .getBody ().trim ();
257235 if (Flickr .debugStream ) {
258236 logger .debug (strXml );
@@ -263,7 +241,7 @@ private Response handleResponse(OAuthRequest request, OAuth10aService service) t
263241
264242 DocumentBuilder builder = getDocumentBuilder ();
265243 Document document = builder .parse (new InputSource (new StringReader (strXml )));
266- f4jResponse = (Response ) responseClass .newInstance ();
244+ Response f4jResponse = (Response ) responseClass . getConstructor () .newInstance ();
267245 f4jResponse .parse (document );
268246
269247 return f4jResponse ;
@@ -314,55 +292,6 @@ public Response getNonOAuth(String path, Map<String, String> parameters) {
314292 }
315293 }
316294
317- /**
318- * @param sharedSecret
319- * @return
320- */
321- private OAuth10aService createOAuthService (String apiKey , String sharedSecret ) {
322- JDKHttpClientConfig config = JDKHttpClientConfig .defaultConfig ();
323- if (connectTimeoutMs != null ) {
324- config .setConnectTimeout (connectTimeoutMs );
325- }
326- if (readTimeoutMs != null ) {
327- config .setReadTimeout (readTimeoutMs );
328- }
329- ServiceBuilder serviceBuilder = new ServiceBuilder (apiKey ).apiKey (apiKey ).apiSecret (sharedSecret ).httpClientConfig (config );
330-
331- if (Flickr .debugRequest ) {
332- serviceBuilder = serviceBuilder .debug ();
333- }
334-
335- return serviceBuilder .build (FlickrApi .instance ());
336- }
337-
338- /**
339- * @param parameters
340- * @param request
341- */
342- private void buildNormalPostRequest (Map <String , Object > parameters , OAuthRequest request ) {
343- for (Map .Entry <String , Object > entry : parameters .entrySet ()) {
344- request .addBodyParameter (entry .getKey (), String .valueOf (entry .getValue ()));
345- }
346- }
347-
348- /**
349- * @param parameters
350- * @param request
351- */
352- private void buildMultipartRequest (Map <String , String > parameters , OAuthRequest request ) {
353- String multipartBoundary = getMultipartBoundary ();
354- request .initMultipartBoundary (multipartBoundary );
355-
356- request .addHeader ("Content-Type" , "multipart/form-data; boundary=" + multipartBoundary );
357- for (Map .Entry <String , String > entry : parameters .entrySet ()) {
358- request .addQuerystringParameter (entry .getKey (), entry .getValue ());
359- }
360- }
361-
362- private String getMultipartBoundary () {
363- return "---------------------------" + UUID .randomUUID ();
364- }
365-
366295 public boolean isProxyAuth () {
367296 return proxyAuth ;
368297 }
0 commit comments