Java Code Examples for org.jboss.resteasy.client.ClientResponse#getStatus()

The following examples show how to use org.jboss.resteasy.client.ClientResponse#getStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActiveMQPushStrategy.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
protected void initialize() throws Exception {
   super.start();
   initialized = true;
   initAuthentication();
   ClientRequest request = executor.createRequest(registration.getTarget().getHref());
   for (XmlHttpHeader header : registration.getHeaders()) {
      request.header(header.getName(), header.getValue());
   }
   ClientResponse<?> res = request.head();
   if (res.getStatus() != 200) {
      throw new RuntimeException("Failed to query REST destination for init information.  Status: " + res.getStatus());
   }
   String url = (String) res.getHeaders().getFirst("msg-create-with-id");
   if (url == null) {
      if (res.getLinkHeader() == null) {
         throw new RuntimeException("Could not find create-with-id URL");
      }
      Link link = res.getLinkHeader().getLinkByTitle("create-with-id");
      if (link == null) {
         throw new RuntimeException("Could not find create-with-id URL");
      }
      url = link.getHref();
   }
   targetUri = ResteasyUriBuilder.fromTemplate(url);
}
 
Example 2
Source File: PostOrderWithId.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   if (args.length < 1 || args[0] == null)
      throw new RuntimeException("You must pass in a parameter");

   // first get the create URL for the shipping queue
   ClientRequest request = new ClientRequest("http://localhost:8080/queues/orders");
   ClientResponse res = request.head();
   Link create = res.getHeaderAsLink("msg-create-with-id");

   Order order = new Order();
   order.setName(args[0]);
   order.setItem("iPhone4");
   order.setAmount("$199.99");

   res = create.request().pathParameter("id", args[0]).body("application/xml", order).post();

   if (res.getStatus() != 201)
      throw new RuntimeException("Failed to post");

   System.out.println("Sent order " + args[0]);
}
 
Example 3
Source File: ReceiveOrder.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   // first get the create URL for the shipping queue
   ClientRequest request = new ClientRequest("http://localhost:8080/queues/orders");
   ClientResponse res = request.head();
   Link pullConsumers = res.getHeaderAsLink("msg-pull-consumers");
   res.releaseConnection();
   res = pullConsumers.request().post();
   Link consumeNext = res.getHeaderAsLink("msg-consume-next");
   res.releaseConnection();
   while (true) {
      System.out.println("Waiting...");
      res = consumeNext.request().header("Accept-Wait", "10").post();
      if (res.getStatus() == 503) {
         System.out.println("Timeout...");
         consumeNext = res.getHeaderAsLink("msg-consume-next");
      } else if (res.getStatus() == 200) {
         Order order = (Order) res.getEntity(Order.class);
         System.out.println(order);
         consumeNext = res.getHeaderAsLink("msg-consume-next");
      } else {
         throw new RuntimeException("Failure! " + res.getStatus());
      }
      res.releaseConnection();
   }
}
 
Example 4
Source File: RestSend.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   // first get the create URL for the shipping queue
   ClientRequest request = new ClientRequest("http://localhost:8080/queues/orders");
   ClientResponse res = request.head();
   Link create = res.getHeaderAsLink("msg-create");

   System.out.println("Send order...");
   Order order = new Order();
   order.setName("Bill");
   order.setItem("iPhone4");
   order.setAmount("$199.99");

   res = create.request().body("application/xml", order).post();
   if (res.getStatus() != 201)
      throw new RuntimeException("Failed to post");
}
 
Example 5
Source File: RestReceive.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
   // first get the create URL for the shipping queue
   ClientRequest request = new ClientRequest("http://localhost:8080/queues/orders");
   ClientResponse res = request.head();
   Link pullConsumers = res.getHeaderAsLink("msg-pull-consumers");
   res = pullConsumers.request().formParameter("autoAck", "false").post();
   Link ackNext = res.getHeaderAsLink("msg-acknowledge-next");
   res.releaseConnection();
   while (true) {
      System.out.println("Waiting...");
      res = ackNext.request().header("Accept-Wait", "10").header("Accept", "application/xml").post();
      if (res.getStatus() == 503) {
         System.out.println("Timeout...");
         ackNext = res.getHeaderAsLink("msg-acknowledge-next");
      } else if (res.getStatus() == 200) {
         Order order = (Order) res.getEntity(Order.class);
         System.out.println(order);
         Link ack = res.getHeaderAsLink("msg-acknowledgement");
         res = ack.request().formParameter("acknowledge", "true").post();
         ackNext = res.getHeaderAsLink("msg-acknowledge-next");
      } else {
         throw new RuntimeException("Failure! " + res.getStatus());
      }
      res.releaseConnection();
   }
}
 
Example 6
Source File: JwtUtil.java    From oxAuth with MIT License 6 votes vote down vote up
public static JSONObject getJSONWebKeys(String jwksUri, ClientExecutor executor) {
    log.debug("Retrieving jwks " + jwksUri + "...");

    JSONObject jwks = null;
    try {
        if (!StringHelper.isEmpty(jwksUri)) {
            ClientRequest clientRequest = executor != null ? new ClientRequest(jwksUri, executor) : new ClientRequest(jwksUri);
            clientRequest.setHttpMethod(HttpMethod.GET);
            ClientResponse<String> clientResponse = clientRequest.get(String.class);

            int status = clientResponse.getStatus();
            log.debug(String.format("Status: %n%d", status));

            if (status == 200) {
                jwks = fromJson(clientResponse.getEntity(String.class));
                log.debug(String.format("JWK: %s", jwks));
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return jwks;
}
 
Example 7
Source File: HttpUtils.java    From pnc with Apache License 2.0 5 votes vote down vote up
/**
 * Process HTTP requests and tests if server responds with expected HTTP code. Request is implicitly set to accept
 * MIME type application/json.
 *
 * @param ecode Expected HTTP error code
 * @param url Request URL
 * @throws Exception Thrown if some error occurs in communication with server
 */
public static void testResponseHttpCode(int ecode, String url) throws Exception {
    ClientRequest request = new ClientRequest(url);
    request.accept(MediaType.APPLICATION_JSON);

    ClientResponse<String> response = request.get(String.class);
    if (response.getStatus() != ecode)
        throw new Exception("Server returned unexpected HTTP code! Returned code:" + response.getStatus());
}
 
Example 8
Source File: BaseResponse.java    From oxAuth with MIT License 5 votes vote down vote up
public BaseResponse(ClientResponse<String> clientResponse) {
    if (clientResponse != null) {
        status = clientResponse.getStatus();
        if (clientResponse.getLocationLink() != null) {
            location = clientResponse.getLocationLink().getHref();
        }
        entity = clientResponse.getEntity(String.class);
        headers = clientResponse.getMetadata();
    }
}
 
Example 9
Source File: PostOrder.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
   // first get the create URL for the shipping queue
   ClientRequest request = new ClientRequest("http://localhost:8080/queues/orders");
   ClientResponse res = request.head();
   Link create = res.getHeaderAsLink("msg-create");

   System.out.println("Send Bill's order...");
   Order order = new Order();
   order.setName("Bill");
   order.setItem("iPhone4");
   order.setAmount("$199.99");

   res = create.request().body("application/xml", order).post();

   if (res.getStatus() == 307) {
      Link redirect = res.getLocationLink();
      res.releaseConnection();
      res = redirect.request().body("application/xml", order).post();
   }

   if (res.getStatus() != 201)
      throw new RuntimeException("Failed to post");

   create = res.getHeaderAsLink("msg-create-next");

   if (res.getStatus() != 201)
      throw new RuntimeException("Failed to post");

   System.out.println("Send Monica's order...");
   order.setName("Monica");

   res.releaseConnection();
   res = create.request().body("application/xml", order).post();

   if (res.getStatus() != 201)
      throw new RuntimeException("Failed to post");

   System.out.println("Resend Monica's order over same create-next link...");

   res.releaseConnection();
   res = create.request().body("application/xml", order).post();

   if (res.getStatus() != 201)
      throw new RuntimeException("Failed to post");
}
 
Example 10
Source File: CIBARegisterParamsValidatorService.java    From oxAuth with MIT License 4 votes vote down vote up
public boolean validateParams(
        BackchannelTokenDeliveryMode backchannelTokenDeliveryMode, String backchannelClientNotificationEndpoint,
        AsymmetricSignatureAlgorithm backchannelAuthenticationRequestSigningAlg, Boolean backchannelUserCodeParameter,
        List<GrantType> grantTypes, SubjectType subjectType, String sectorIdentifierUri, String jwks, String jwksUri) {
    try {
        // Not CIBA Registration
        if (backchannelTokenDeliveryMode == null && Strings.isBlank(backchannelClientNotificationEndpoint) && backchannelAuthenticationRequestSigningAlg == null) {
            return true;
        }

        // Required parameter.
        if (backchannelTokenDeliveryMode == null
                || !appConfiguration.getBackchannelTokenDeliveryModesSupported().contains(backchannelTokenDeliveryMode.getValue())) {
            return false;
        }

        // Required if the token delivery mode is set to ping or push.
        if ((backchannelTokenDeliveryMode == PING || backchannelTokenDeliveryMode == PUSH)
                && Strings.isBlank(backchannelClientNotificationEndpoint)) {
            return false;
        }

        // Grant type urn:openid:params:grant-type:ciba is required if the token delivery mode is set to ping or poll.
        if (backchannelTokenDeliveryMode == PING || backchannelTokenDeliveryMode == POLL) {
            if (!appConfiguration.getGrantTypesSupported().contains(CIBA) || !grantTypes.contains(CIBA)) {
                return false;
            }
        }

        // If the server does not support backchannel_user_code_parameter_supported, the default value is false.
        if (appConfiguration.getBackchannelUserCodeParameterSupported() == null || appConfiguration.getBackchannelUserCodeParameterSupported() == false) {
            backchannelUserCodeParameter = false;
        }

        if (subjectType != null && subjectType == SubjectType.PAIRWISE) {

            if (backchannelTokenDeliveryMode == PING || backchannelTokenDeliveryMode == POLL) {
                if (Strings.isBlank(jwks) && Strings.isBlank(jwksUri)) {
                    return false;
                }
            }

            if (Strings.isNotBlank(sectorIdentifierUri)) {
                ClientRequest clientRequest = new ClientRequest(sectorIdentifierUri);
                clientRequest.setHttpMethod(HttpMethod.GET);

                ClientResponse<String> clientResponse = clientRequest.get(String.class);
                int status = clientResponse.getStatus();

                if (status != 200) {
                    return false;
                }

                String entity = clientResponse.getEntity(String.class);
                JSONArray sectorIdentifierJsonArray = new JSONArray(entity);

                if (backchannelTokenDeliveryMode == PING || backchannelTokenDeliveryMode == POLL) {
                    // If a sector_identifier_uri is explicitly provided, then the jwks_uri must be included in the list of
                    // URIs pointed to by the sector_identifier_uri.
                    if (!Strings.isBlank(jwksUri) && !Util.asList(sectorIdentifierJsonArray).contains(jwksUri)) {
                        return false;
                    }
                } else if (backchannelTokenDeliveryMode == PUSH) {
                    // In case a sector_identifier_uri is explicitly provided, then the backchannel_client_notification_endpoint
                    // must be included in the list of URIs pointed to by the sector_identifier_uri.
                    if (!Util.asList(sectorIdentifierJsonArray).contains(backchannelClientNotificationEndpoint)) {
                        return false;
                    }
                }
            }
        }
    } catch (Exception e) {
        log.trace(e.getMessage(), e);
        return false;
    }

    return true;
}
 
Example 11
Source File: RedirectionUriService.java    From oxAuth with MIT License 4 votes vote down vote up
public String validateRedirectionUri(@NotNull Client client, String redirectionUri) {
    try {
        String sectorIdentifierUri = client.getSectorIdentifierUri();
        String[] redirectUris = client.getRedirectUris();

        if (StringUtils.isNotBlank(sectorIdentifierUri)) {
            ClientRequest clientRequest = new ClientRequest(sectorIdentifierUri);
            clientRequest.setHttpMethod(HttpMethod.GET);

            ClientResponse<String> clientResponse = clientRequest.get(String.class);
            int status = clientResponse.getStatus();

            if (status == 200) {
                String entity = clientResponse.getEntity(String.class);
                JSONArray sectorIdentifierJsonArray = new JSONArray(entity);
                redirectUris = new String[sectorIdentifierJsonArray.length()];
                for (int i = 0; i < sectorIdentifierJsonArray.length(); i++) {
                    redirectUris[i] = sectorIdentifierJsonArray.getString(i);
                }
            } else {
                return null;
            }
        }

        if (StringUtils.isNotBlank(redirectionUri) && redirectUris != null) {
            log.debug("Validating redirection URI: clientIdentifier = {}, redirectionUri = {}, found = {}",
                    client.getClientId(), redirectionUri, redirectUris.length);

            if (isUriEqual(redirectionUri, redirectUris)) {
                return redirectionUri;
            }
        } else {
            // Accept Request Without redirect_uri when One Registered
            if (redirectUris != null && redirectUris.length == 1) {
                return redirectUris[0];
            }
        }
    } catch (Exception e) {
        return null;
    }
    return null;
}
 
Example 12
Source File: JwtUtil.java    From oxAuth with MIT License 4 votes vote down vote up
public static JSONObject getJsonKey(String jwksUri, String jwks, String keyId) {
    log.debug("Retrieving JWK Key...");

    JSONObject jsonKey = null;
    try {
        if (StringHelper.isEmpty(jwks)) {
            ClientRequest clientRequest = new ClientRequest(jwksUri);
            clientRequest.setHttpMethod(HttpMethod.GET);
            ClientResponse<String> clientResponse = clientRequest.get(String.class);

            int status = clientResponse.getStatus();
            log.debug(String.format("Status: %n%d", status));

            if (status == 200) {
                jwks = clientResponse.getEntity(String.class);
                log.debug(String.format("JWK: %s", jwks));
            }
        }
        if (StringHelper.isNotEmpty(jwks)) {
            JSONObject jsonObject = new JSONObject(jwks);
            JSONArray keys = jsonObject.getJSONArray(JSON_WEB_KEY_SET);
            if (keys.length() > 0) {
                if (StringHelper.isEmpty(keyId)) {
                    jsonKey = keys.getJSONObject(0);
                } else {
                    for (int i = 0; i < keys.length(); i++) {
                        JSONObject kv = keys.getJSONObject(i);
                        if (kv.getString(KEY_ID).equals(keyId)) {
                            jsonKey = kv;
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return jsonKey;
}