private static void completeOperationOutput(
      CollectInfo collectInfo,
      Operation operation,
      MethodAnnotationInfo mai,
      List<? extends IntrospectionHelper> introspectionHelper) {
    Response response = new Response();

    if (mai.getJavaMethod().getReturnType() != Void.TYPE) {
      TypeInfo outputTypeInfo;
      try {
        outputTypeInfo =
            Types.getTypeInfo(
                mai.getJavaMethod().getReturnType(), mai.getJavaMethod().getGenericReturnType());
      } catch (UnsupportedTypeException e) {
        LOGGER.warning(
            "Could not add output representation of method "
                + mai.getJavaMethod()
                + ". "
                + e.getMessage());
        return;
      }
      // Output representation
      RepresentationCollector.addRepresentation(collectInfo, outputTypeInfo, introspectionHelper);

      PayLoad outputEntity = new PayLoad();
      outputEntity.setType(outputTypeInfo.getRepresentationName());
      outputEntity.setArray(outputTypeInfo.isList());

      response.setOutputPayLoad(outputEntity);

      response.setCode(Status.SUCCESS_OK.getCode());
      response.setName(Status.SUCCESS_OK.getReasonPhrase());
      response.setDescription("");
      response.setMessage(Status.SUCCESS_OK.getDescription());
    } else {
      response.setCode(Status.SUCCESS_NO_CONTENT.getCode());
      response.setName(Status.SUCCESS_NO_CONTENT.getReasonPhrase());
      response.setDescription("");
      response.setMessage(Status.SUCCESS_NO_CONTENT.getDescription());
    }

    operation.getResponses().add(response);
  }
Exemplo n.º 2
0
  /**
   * Handles any call to this resource. The default implementation check the {@link
   * #isConditional()} and {@link #isNegotiated()} method to determine which one of the {@link
   * #doConditionalHandle()}, {@link #doNegotiatedHandle()} and {@link #doHandle()} methods should
   * be invoked. It also catches any {@link ResourceException} thrown and updates the response
   * status using the {@link #setStatus(Status, Throwable, String)} method.<br>
   * <br>
   * After handling, if the status is set to {@link Status#CLIENT_ERROR_METHOD_NOT_ALLOWED}, then
   * {@link #updateAllowedMethods()} is invoked to give the resource a chance to inform the client
   * about the allowed methods.
   *
   * @return The response entity.
   */
  @Override
  public Representation handle() {
    Representation result = null;

    // If the resource is not available after initialization and if this a
    // retrieval method, then return a "not found" response.
    if (!isExisting() && getMethod().isSafe()) {
      setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    } else {
      try {
        if (isConditional()) {
          result = doConditionalHandle();
        } else if (isNegotiated()) {
          result = doNegotiatedHandle();
        } else {
          result = doHandle();
        }

        if (!getResponse().isEntityAvailable()) {
          // If the user manually set the entity, keep it
          getResponse().setEntity(result);
        }

        if (Status.CLIENT_ERROR_METHOD_NOT_ALLOWED.equals(getStatus())) {
          updateAllowedMethods();
        } else if (Method.GET.equals(getMethod())
            && Status.SUCCESS_OK.equals(getStatus())
            && (getResponseEntity() == null || !getResponseEntity().isAvailable())) {
          getLogger()
              .fine(
                  "A response with a 200 (Ok) status should have an entity. Changing the status to 204 (No content).");
          setStatus(Status.SUCCESS_NO_CONTENT);
        }
      } catch (Throwable t) {
        doCatch(t);
      }
    }

    return result;
  }