private MyResult invokeCallbackString(String url) { Client client = newClient(); WebTarget target = client.target(url); final AsyncInvoker asyncInvoker = target.request().async(); final MyResult myResponse = new MyResult(); asyncInvoker.get(new InvocationCallback<String>() { @Override public void completed(String response) { myResponse.setResponse(response); myResponse.setOk(true); } @Override public void failed(Throwable arg0) { myResponse.setResponse(arg0.getMessage()); myResponse.setOk(false); } }); try { sleep(2000); } catch (InterruptedException e) { logger.log(SEVERE, "error", e); } return myResponse; }
private MyResult invokeCallbackResponse(String url) { Client client = newClient(); WebTarget target = client.target(url); final AsyncInvoker asyncInvoker = target.request().async(); final MyResult myResponse = new MyResult(); asyncInvoker.get(new InvocationCallback<Response>() { @Override public void completed(Response response) { myResponse.setResponse(response.readEntity(String.class)); myResponse.setOk(response.hasEntity()); } @Override public void failed(Throwable arg0) { myResponse.setResponse(arg0.getMessage()); myResponse.setOk(false); } }); try { sleep(2000); } catch (InterruptedException e) { logger.log(SEVERE, "error", e); } return myResponse; }
private AsyncInvoker prepareRequest(ForPage forPage, RequestDetails entity, String... additionalPaths) { WebTarget localTarget = prepareTarget(forPage, entity, additionalPaths); Invocation.Builder builder = localTarget.request().accept(MediaType.APPLICATION_JSON_TYPE); if (configuration.getApiToken() != null) { builder = builder.header("X-AUTH-TOKEN", configuration.getApiToken()); } if ((entity != null) && entity.canBeRetried()) { builder = builder.property(RetryConnector.PROPERTY_ENABLE, true); } return builder.async(); }
/** * Posts a request to the Omniture API to get a report back. Reports may take a while to generate, * so this will loop on the Report.Get request, and ignore any errors indicating that the report * is not yet ready. * @param reportId ID of report to get * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException * @throws IOException */ public void getReport(int reportId) throws InterruptedException, ExecutionException, TimeoutException, IOException, StageException { int waitTime = 1000; Response response = null; while (!stop) { final AsyncInvoker asyncInvoker = getResource.request() .header(WSSE_HEADER, OmnitureAuthUtil.getHeader(username.get(), sharedSecret.get())) .async(); LOG.debug("Getting report using URL {} with report ID {}", getResource.getUri().toURL().toString(), reportId); final Future<Response> responseFuture = asyncInvoker.post(Entity.json("{ \"reportID\": " + reportId + " }")); response = responseFuture.get(responseTimeoutMillis, TimeUnit.MILLISECONDS); String input = response.readEntity(String.class); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(input); // If the report has an error field, it means the report has not finished generating if (!root.has("error")) { boolean accepted = entityQueue.offer(input, responseTimeoutMillis, TimeUnit.MILLISECONDS); if (!accepted) { LOG.warn("Response buffer full, dropped record."); } break; } else { // Exponential backoff while making subsequent Report.Get requests if (root.get("error").textValue().equals("report_not_ready")) { waitTime *= 2; LOG.info("Report not available. Sleeping for {} seconds", waitTime / 1000); Thread.sleep(waitTime); } else { throw new StageException(Errors.OMNITURE_02, root.get("error").get("error_description").asText()); } } } response.close(); }
@VisibleForTesting protected <T> Future<T> invokePreparedRequest(RequestDetails entity, InvocationCallback<T> callback, AsyncInvoker invoker) { if (entity != null) { if (entity.getEntity() != null) { return invoker.method(entity.getMethod().name(), Entity.entity(entity.getEntity(), MediaType.APPLICATION_JSON_TYPE), callback); } return invoker.method(entity.getMethod().name(), callback); } return invoker.get(callback); }
@VisibleForTesting protected <T> Future<T> invokePreparedRequest(RequestDetails entity, Class<T> responseType, AsyncInvoker invoker) { if (entity != null) { if (entity.getEntity() != null) { return invoker.method(entity.getMethod().name(), Entity.entity(entity.getEntity(), MediaType.APPLICATION_JSON_TYPE), responseType); } return invoker.method(entity.getMethod().name(), responseType); } return invoker.get(responseType); }
@VisibleForTesting protected <T> Future<T> invokePreparedRequest(RequestDetails entity, GenericType<T> responseType, AsyncInvoker invoker) { if (entity != null) { if (entity.getEntity() != null) { return invoker.method(entity.getMethod().name(), Entity.entity(entity.getEntity(), MediaType.APPLICATION_JSON_TYPE), responseType); } return invoker.method(entity.getMethod().name(), responseType); } return invoker.get(responseType); }
@Override public AsyncInvoker async() { throw new UnsupportedOperationException(); }
public AsyncInvoker async() { // TODO: implement wrapping return delegate.async(); }
@Override public <T> Future<T> invokeRequest(ForPage forPage, RequestDetails requestDetails, Class<T> responseType, String... additionalPaths) { AsyncInvoker invoker = prepareRequest(forPage, requestDetails, additionalPaths); return invokePreparedRequest(requestDetails, responseType, invoker); }
@Override public <T> Future<T> invokeRequest(ForPage forPage, RequestDetails requestDetails, GenericType<T> responseType, String... additionalPaths) { AsyncInvoker invoker = prepareRequest(forPage, requestDetails, additionalPaths); return invokePreparedRequest(requestDetails, responseType, invoker); }
@Override public <T> Future<T> invokeRequest(ForPage forPage, RequestDetails requestDetails, InvocationCallback<T> callback, String... additionalPaths) { AsyncInvoker invoker = prepareRequest(forPage, requestDetails, additionalPaths); return invokePreparedRequest(requestDetails, callback, invoker); }
public Object call (HttpMethods method, URI uri, String requestContentType, String responseContentType, Object data, JiveAuthorizationSupport authorization, JiveRunAs runAs, Map<String,String> additionalHeaders, Class clazz) throws JiveClientException { /**** NEED TO MAKE SURE ****/ if (requestContentType == null) { requestContentType = MediaType.APPLICATION_JSON; } // end if if (clazz == null) { clazz = Object.class; } // end if Client client = buildClient(); WebTarget target = client.target(uri); AsyncInvoker invoker = getAsyncInvoker(target, requestContentType, authorization, runAs,additionalHeaders); Future<Object> responseFuture = null; if (data != null) { Entity entity = Entity.entity(data, MediaType.APPLICATION_JSON_TYPE); responseFuture = invoker.method(method.name(),entity,clazz); } else { responseFuture = invoker.method(method.name(),clazz); } // end if Object response = null; try { response = responseFuture.get(); //TODO: DETERMINE STATUS CODE?? if (log.isInfoEnabled()) { log.info("Successful Called ["+uri+"] ..."); } return response; } catch (BadRequestException bre) { log.error("Error Calling API [" + uri + "]", bre); throw JiveClientException.buildException("Error Calling API [" + uri + "]",bre,null,data,data.getClass()); } catch (InterruptedException ie) { log.error("Error Calling API [" + uri + "]", ie); throw JiveClientException.buildException("Error Calling API [" + uri + "]",ie,null,data,data.getClass()); } catch (ExecutionException ee) { log.error("Error Calling API [" + uri + "]", ee); throw JiveClientException.buildException("Error Calling API [" + uri + "]",ee,null,data,data.getClass()); } // end try/catch }
public Object fetchData(TileInstance tileInstance, Class clazz) throws JiveClientException { initAccessTokens(tileInstance); Client client = buildClient(); //TODO: CONFIRM THE URL NEEDED TO FETCH DATA, PRETTY SURE THIS ISNT jivePushURL WebTarget target = client.target(tileInstance.getJivePushUrl()); AsyncInvoker asyncInvoker = target.request(MediaType.APPLICATION_JSON_TYPE) .header(HttpHeaders.AUTHORIZATION,tileInstance.getCredentials().getAuthorizationHeader()) .async(); Future<Response> responseFuture = asyncInvoker.get(); Response response = null; try { response = responseFuture.get(); //TODO: CHECK THAT THIS IS THE CORRECT RESPONSE CODE if (response.getStatus() == 204) { //TODO: CONFIRM THE URL NEEDED TO FETCH DATA, PRETTY SURE THIS ISNT jivePushURL if (log.isInfoEnabled()) { log.info("Successful Fetch ["+tileInstance.getJivePushUrl()+"]"); } //TODO: EXPORT TO UTILITY CLASS //TODO: NEED TO USE JACKSON AND CONVERT ENTITY TO ITS ORIGINAL OBJECT TYPE? return response.getEntity(); } // end if } catch (BadRequestException bre) { log.error("Error Fetching Data From Tile [" + tileInstance.getJivePushUrl() + "]", bre); throw JiveClientException.buildException("Error Fetching Data to Tile [" + tileInstance.getJivePushUrl() + "]",bre,tileInstance,null,clazz); } catch (InterruptedException ie) { log.error("Error Fetching Data From Tile [" + tileInstance.getJivePushUrl() + "]", ie); throw JiveClientException.buildException("Error Fetching Data to Tile [" + tileInstance.getJivePushUrl() + "]",ie,tileInstance,null,clazz); } catch (ExecutionException ee) { log.error("Error Fetching Data From Tile [" + tileInstance.getJivePushUrl() + "]", ee); throw JiveClientException.buildException("Error Fetching Data to Tile [" + tileInstance.getJivePushUrl() + "]",ee,tileInstance,null,clazz); } // end try/catch asyncInvoker = null; target = null; client.close(); client = null; return null; }
public void initAccessTokens(JiveInstance jiveInstance) { if (!JiveSDKUtils.isAllExist(jiveInstance.getCredentials().getRefreshToken())) { if (log.isDebugEnabled()) { log.debug("Refresh Credentials DO NOT Exist, Requesting Access Token..."); } requestAccessTokens(jiveInstance); return; } // end if //TODO: ADD LOGIC TO EXIT IF ACCESS TOKENS ARE STILL VALID Client client = buildClient(); client.register(HttpAuthenticationFeature.basic(jiveInstance.getClientId(), jiveInstance.getClientSecret())); WebTarget target = client.target(jiveInstance.getJiveUrl()).path("/oauth2/token"); Form form = new Form("grant_type", "refresh_token"); form.param("refresh_token", jiveInstance.getCredentials().getAccessToken()); form.param("client_id", jiveInstance.getClientId()); form.param("client_secret", jiveInstance.getClientSecret()); //TODO: IMPLEMENT FUTURES AsyncInvoker asyncInvoker = target.request(MediaType.APPLICATION_FORM_URLENCODED).async(); Future<JiveOAuthResponse> responseFuture = asyncInvoker.post(Entity.entity(form, MediaType.APPLICATION_JSON_TYPE),JiveOAuthResponse.class); JiveOAuthResponse response = null; try { response = responseFuture.get(); } catch (BadRequestException bre) { log.error("Error Getting OAuth Access Tokens [/oauth2/token]", bre); } catch (InterruptedException ie) { log.error("Error Getting OAuth Access Tokens [/oauth2/token]", ie); } catch (ExecutionException ee) { log.error("Error Getting OAuth Access Tokens [/oauth2/token]", ee); } finally { response = null; } // end try/catch responseFuture = null; form = null; target = null; client.close(); client = null; // BACKUP // JiveOAuthResponse response = target.request(MediaType.APPLICATION_FORM_URLENCODED).post(Entity.entity(form, MediaType.APPLICATION_JSON_TYPE),JiveOAuthResponse.class); if (log.isDebugEnabled()) { log.debug("Refresh Token : Response : " + response); } OAuthCredentials credentials = new OAuthCredentials(); credentials.setAccessToken(response.getAccessToken()); credentials.setRefreshToken(response.getRefreshToken()); jiveInstance.setCredentials(credentials); }
AsyncInvoker get();