All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.directory.scim.client.rest.RestClientUtil Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*
 * The Pennsylvania State University © 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.directory.scim.client.rest;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.Response.Status.Family;
import org.apache.directory.scim.spec.resources.ScimResource;
import org.springframework.http.ResponseEntity;


public final class RestClientUtil {

  private RestClientUtil() {

  }

  public static void checkForSuccess(Response response) throws RestException{
    if (!isSuccessful(response)) {
      throw new RestException(response);
    }
  }
  
  public static boolean checkForFourOhFour(WebTarget target, Response response) {
    try {
      verifyNotFourOhFour(target, response);
      return false;
    } catch (RestException e) {
      return true;
    }
  }
  
  public static void verifyNotFourOhFour(WebTarget target, Response response) throws RestException {
    if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) {
      throw new RestException(response);
    }
  }

  public static boolean isSuccessful(Response response) {
    boolean isSuccessful;
    Family responseFamily = response.getStatusInfo()
                                    .getFamily();
    isSuccessful = responseFamily != Family.CLIENT_ERROR && responseFamily != Family.SERVER_ERROR;

    return isSuccessful;
  }

  /**
   * Closes response and suppresses any known/expected exceptions
   * from closing it.
   * 
   * @param response
   */
  public static void close(Response response) {
    try {
      response.close();
    } catch (ProcessingException ignored) {
    }
  }

  /**
   * Closes response and passes any known/expected exceptions from
   * closing it to consumer (e.g. for logging).
   * 
   * @param response
   * @param consumer
   */
  public static void close(Response response, Consumer consumer) {
    try {
      response.close();
    } catch (ProcessingException processingException) {
      consumer.accept(processingException);
    }
  }

  /**
   * Read an entity from the response if it was found and returned.
   * 
   * @param response
   *          the {@link Response} to read from
   * @param entityType
   *          the type of entity
   * @return Optional.empty() if Not Found or empty
   *         response, otherwise Optional.ofNullable(T)
   * @throws RestException
   *           if response is an error response other than
   *           404 Not Found
   * @throws ProcessingException
   *           see {@link Response#readEntity(Class)}
   * @throws IllegalStateException
   *           see {@link Response#readEntity(Class)}
   * @throws RestException
   */
  public  Optional tryReadEntity(Response response, Class entityType) throws RestException, ProcessingException, IllegalStateException{
    return readEntity(response, entityType, response::readEntity, Optional::ofNullable);
  }

  /**
   * Read an entity from the response if it was found and returned.
   * 
   * @param response
   *          the {@link Response} to read from
   * @param entityType
   *          the type of entity
   * @return Optional.empty() if Not Found or empty
   *         response, otherwise Optional.ofNullable(T)
   * @throws RestException
   *           if response is an error response other than
   *           404 Not Found
   * @throws ProcessingException
   *           see {@link Response#readEntity(GenericType)}
   * @throws IllegalStateException
   *           see {@link Response#readEntity(GenericType)}
   * @throws RestException
   */
  public  Optional tryReadEntity(Response response, GenericType entityType) throws RestException, ProcessingException, IllegalStateException {
    return readEntity(response, entityType, response::readEntity, Optional::ofNullable);
  }

  /**
   * 

* Read an entity from the response if it was found. *

*

* Useful for REST endpoints that MUST return an entity. *

* * @param response * the {@link Response} to read from * @param entityType * the type of entity * @return Optional.empty() if Not Found, otherwise * Optional.of(T) * @throws RestException * if response is an error response other than * 404 Not Found * @throws ProcessingException * see {@link Response#readEntity(Class)} * @throws IllegalStateException * see {@link Response#readEntity(Class)} * @throws RestException */ public static Optional readEntity(Response response, Class entityType) throws RestException, ProcessingException, IllegalStateException{ return readEntity(response, entityType, response::readEntity, Optional::of); } /** *

* Read an entity from the response if it was found. *

*

* Useful for REST endpoints that MUST return an entity. *

* * @param response * the {@link Response} to read from * @param entityType * the type of entity * @return Optional.empty() if Not Found, otherwise * Optional.of(T) * @throws RestException * if response is an error response other than * 404 Not Found * @throws ProcessingException * see {@link Response#readEntity(GenericType)} * @throws IllegalStateException * see {@link Response#readEntity(GenericType)} * @throws RestException */ public static Optional readEntity(Response response, GenericType entityType) throws RestException, ProcessingException, IllegalStateException{ return readEntity(response, entityType, response::readEntity, Optional::of); } private static Optional readEntity(Response response, T entityType, Function readEntity, Function> optionalOf) throws RestException, ProcessingException, IllegalStateException{ Optional result; if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) { result = Optional.empty(); } else { checkForSuccess(response); E responseEntity = readEntity.apply(entityType); result = optionalOf.apply(responseEntity); } return result; } public static Optional extractIdFromLocationTag(Response response) { String location = response.getHeaderString("Location"); if (location == null) { return Optional.empty(); } String[] uriParts = location.split("/"); Integer nbrParts = uriParts.length; return Optional.of(uriParts[nbrParts - 1]); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy