agave-uuids/uuids-api/src/main/java/org/iplantc/service/uuid/resource/impl/AbstractUuidResource.java
/**
	 * Fetches the resource at the given URL using a HTTP Get and pass through
	 * auth headers.
	 * 
	 * @param resourceUrl the url to the resource requested
	 * @param filter the url filter to use on the remote request
	 * @param headerMap the auth and client headers used to request the resource
	 * @return JsonNode representation of the naked response
	 * @throws UUIDResolutionException if unable to resolve the uuid to a valid resource
	 */
Fetches the resource at the given URL using a HTTP Get and pass through
auth headers.


Fetches the resource at the given URL using a HTTP Get and pass through auth headers .
protected JsonNode fetchResource(String resourceUrl, String filter, Map<String,String> headerMap) throws UUIDResolutionException {
		ObjectMapper mapper = new ObjectMapper();
		JsonNode attemptResponse;
		String internalUrl = null;
		try 
		{
			internalUrl = getInternalResourceUrl(resourceUrl);
			if (StringUtils.isNotEmpty(filter)) {
				if (StringUtils.contains(internalUrl, "?")) {
					internalUrl += "&filter="+URLEncoder.encode(StringUtils.stripToEmpty(filter), "utf-8");
				}
				else {
					internalUrl += "?filter="+URLEncoder.encode(StringUtils.stripToEmpty(filter), "utf-8");
				}
			}
			log.debug(String.format("Resolving %s to %s", resourceUrl, internalUrl));
			URI escapedUri = URI.create(internalUrl);
			CloseableHttpClient httpclient;
			if (escapedUri.getScheme().equalsIgnoreCase("https"))
			{
				SSLContext sslContext = new SSLContextBuilder()
						.loadTrustMaterial(null, new TrustStrategy() {
		                    public boolean isTrusted(
		                            final X509Certificate[] chain, final String authType) throws CertificateException {
		                        return true;
		                    }
		                })
						.useTLS()
						.build();
			    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
			    		sslContext,
			    		new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"},   
			    	    null,
			    		SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			    httpclient = HttpClients.custom()
			    		.setSSLSocketFactory(sslsf)
			    		.setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
			    		.build();
			}
			else {
				httpclient = HttpClients.custom()
						.setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
			    		.build();
			}
			HttpGet httpGet = new HttpGet(escapedUri);
			RequestConfig config = RequestConfig.custom()
												.setConnectTimeout(30000)
												.setSocketTimeout(30000)
												.build();
			httpGet.setConfig(config);
			for (String headerName: headerMap.keySet()) {
				httpGet.addHeader(headerName, headerMap.get(headerName));
	        }
			long callstart = System.currentTimeMillis();
			try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
				InputStream in = null;
				try {
					int statusCode = response.getStatusLine().getStatusCode();
					HttpEntity entity = response.getEntity();
					long contentLength = entity.getContentLength();
					if (contentLength > 0 || contentLength == -1) {
						in = entity.getContent();
						attemptResponse = mapper.readTree(in);
						if (statusCode >= 200 && statusCode <= 300) {
							if (attemptResponse.hasNonNull("result")) {
								if (attemptResponse.get("result").isArray()) {
									return attemptResponse.get("result").get(0);
								} else {
									return attemptResponse.get("result");
								}
							} else {
								return mapper.createObjectNode();
							}
						}
						else {
							if (attemptResponse.hasNonNull("version"))
								((ObjectNode) attemptResponse).remove("version");
							if (attemptResponse.hasNonNull("result"))
								((ObjectNode) attemptResponse).remove("result");
							return attemptResponse;
						}
					}
					else {
						return mapper.createObjectNode();
					}
				}
				catch (Exception e) {
					int statusCode = 0;
					String message = null;
					if (response != null) {
						StatusLine statusLine = response.getStatusLine();
						if (statusLine != null) {
							statusCode = statusLine.getStatusCode();
							message = statusLine.getReasonPhrase();
						}
					}
					throw new UUIDResolutionException("Failed to resolve " + internalUrl +
							". Server responded with: " + statusCode + " - " + message, e);
				} finally {
					try {
						assert in != null;
						in.close();
					} catch (Exception ignore) {}
				}
			} catch (ConnectTimeoutException e) {
				throw new UUIDResolutionException("Failed to resolve " + resourceUrl +
						". Remote call to " + escapedUri + " timed out after " +
						(System.currentTimeMillis() - callstart) + " milliseconds.", e);
			} catch (SSLException e) {
				if (StringUtils.equalsIgnoreCase(escapedUri.getScheme(), "https")) {
					throw new UUIDResolutionException("Failed to resolve " + resourceUrl +
							". Remote call to " + escapedUri + " failed due to the remote side not supporting SSL.", e);
				} else {
					throw new UUIDResolutionException("Failed to resolve " + resourceUrl +
							". Remote call to " + escapedUri + " failed due a server side SSL failure.");
				}
			}
		}
		catch (UUIDResolutionException e) {
			throw e;
		}
		catch (Throwable e) {
			throw new UUIDResolutionException("Failed to resolve " + resourceUrl + 
					" due to internal server error.");
		} 
	}
protected JsonNode fetchResource ( String resourceUrl , String filter , Map < String , String > headerMap ) throws UUIDResolutionException { ObjectMapper mapper = new ObjectMapper ( ) ; JsonNode attemptResponse ; String internalUrl = null ; try { internalUrl = getInternalResourceUrl ( resourceUrl ) ; if ( StringUtils . isNotEmpty ( filter ) ) { if ( StringUtils . contains ( internalUrl , "?" ) ) { internalUrl += "&filter=" + URLEncoder . encode ( StringUtils . stripToEmpty ( filter ) , "utf-8" ) ; } else { internalUrl += "?filter=" + URLEncoder . encode ( StringUtils . stripToEmpty ( filter ) , "utf-8" ) ; } } log . debug ( String . format ( "Resolving %s to %s" , resourceUrl , internalUrl ) ) ; URI escapedUri = URI . create ( internalUrl ) ; CloseableHttpClient httpclient ; if ( escapedUri . getScheme ( ) . equalsIgnoreCase ( "https" ) ) { SSLContext sslContext = new SSLContextBuilder ( ) . loadTrustMaterial ( null , new TrustStrategy ( ) { public boolean isTrusted ( final X509Certificate [ ] chain , final String authType ) throws CertificateException { return true ; } } ) . useTLS ( ) . build ( ) ; SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory ( sslContext , new String [ ] { "TLSv1" , "TLSv1.1" , "TLSv1.2" } , null , SSLConnectionSocketFactory . ALLOW_ALL_HOSTNAME_VERIFIER ) ; httpclient = HttpClients . custom ( ) . setSSLSocketFactory ( sslsf ) . setRedirectStrategy ( LaxRedirectStrategy . INSTANCE ) . build ( ) ; } else { httpclient = HttpClients . custom ( ) . setRedirectStrategy ( LaxRedirectStrategy . INSTANCE ) . build ( ) ; } HttpGet httpGet = new HttpGet ( escapedUri ) ; RequestConfig config = RequestConfig . custom ( ) . setConnectTimeout ( 30000 ) . setSocketTimeout ( 30000 ) . build ( ) ; httpGet . setConfig ( config ) ; for ( String headerName : headerMap . keySet ( ) ) { httpGet . addHeader ( headerName , headerMap . get ( headerName ) ) ; } long callstart = System . currentTimeMillis ( ) ; try ( CloseableHttpResponse response = httpclient . execute ( httpGet ) ) { InputStream in = null ; try { int statusCode = response . getStatusLine ( ) . getStatusCode ( ) ; HttpEntity entity = response . getEntity ( ) ; long contentLength = entity . getContentLength ( ) ; if ( contentLength > 0 || contentLength == - 1 ) { in = entity . getContent ( ) ; attemptResponse = mapper . readTree ( in ) ; if ( statusCode >= 200 && statusCode <= 300 ) { if ( attemptResponse . hasNonNull ( "result" ) ) { if ( attemptResponse . get ( "result" ) . isArray ( ) ) { return attemptResponse . get ( "result" ) . get ( 0 ) ; } else { return attemptResponse . get ( "result" ) ; } } else { return mapper . createObjectNode ( ) ; } } else { if ( attemptResponse . hasNonNull ( "version" ) ) ( ( ObjectNode ) attemptResponse ) . remove ( "version" ) ; if ( attemptResponse . hasNonNull ( "result" ) ) ( ( ObjectNode ) attemptResponse ) . remove ( "result" ) ; return attemptResponse ; } } else { return mapper . createObjectNode ( ) ; } } catch ( Exception e ) { int statusCode = 0 ; String message = null ; if ( response != null ) { StatusLine statusLine = response . getStatusLine ( ) ; if ( statusLine != null ) { statusCode = statusLine . getStatusCode ( ) ; message = statusLine . getReasonPhrase ( ) ; } } throw new UUIDResolutionException ( "Failed to resolve " + internalUrl + ". Server responded with: " + statusCode + " - " + message , e ) ; } finally { try { assert in != null ; in . close ( ) ; } catch ( Exception ignore ) { } } } catch ( ConnectTimeoutException e ) { throw new UUIDResolutionException ( "Failed to resolve " + resourceUrl + ". Remote call to " + escapedUri + " timed out after " + ( System . currentTimeMillis ( ) - callstart ) + " milliseconds." , e ) ; } catch ( SSLException e ) { if ( StringUtils . equalsIgnoreCase ( escapedUri . getScheme ( ) , "https" ) ) { throw new UUIDResolutionException ( "Failed to resolve " + resourceUrl + ". Remote call to " + escapedUri + " failed due to the remote side not supporting SSL." , e ) ; } else { throw new UUIDResolutionException ( "Failed to resolve " + resourceUrl + ". Remote call to " + escapedUri + " failed due a server side SSL failure." ) ; } } } catch ( UUIDResolutionException e ) { throw e ; } catch ( Throwable e ) { throw new UUIDResolutionException ( "Failed to resolve " + resourceUrl + " due to internal server error." ) ; } }
Fetches the resource at the given URL using a HTTP Get and pass through
 auth headers.
Fetches the resource at the given URL using a HTTP Get and pass through auth headers .
// add the auth and client headers provided to this method // if successful, just return the result from the response // calls to the files api will return an array. the first (and only // since we appended limit=1 to files api queries) object is // always the one we're interested in. // if failed, just return the error message and status from teh response. // if no content is returned, return an empty object.