Package ca.uhn.fhir.rest.api

Examples of ca.uhn.fhir.rest.api.MethodOutcome


    }
  }

  public static MethodOutcome process2xxResponse(FhirContext theContext, String theResourceName, int theResponseStatusCode, String theResponseMimeType, Reader theResponseReader, Map<String, List<String>> theHeaders) {
    List<String> locationHeaders = theHeaders.get("location");
    MethodOutcome retVal = new MethodOutcome();
    if (locationHeaders != null && locationHeaders.size() > 0) {
      String locationHeader = locationHeaders.get(0);
      parseContentLocation(retVal, theResourceName, locationHeader);
    }
    if (theResponseStatusCode != Constants.STATUS_HTTP_204_NO_CONTENT) {
      EncodingEnum ct = EncodingEnum.forContentType(theResponseMimeType);
      if (ct != null) {
        PushbackReader reader = new PushbackReader(theResponseReader);

        try {
          int firstByte = reader.read();
          if (firstByte == -1) {
            ourLog.debug("No content in response, not going to read");
            reader = null;
          } else {
            reader.unread(firstByte);
          }
        } catch (IOException e) {
          ourLog.debug("No content in response, not going to read", e);
          reader = null;
        }

        if (reader != null) {
          IParser parser = ct.newParser(theContext);
          IResource outcome = parser.parseResource(reader);
          if (outcome instanceof OperationOutcome) {
            retVal.setOperationOutcome((OperationOutcome) outcome);
          }
        }

      } else {
        ourLog.debug("Ignoring response content of type: {}", theResponseMimeType);
View Full Code Here


    case Constants.STATUS_HTTP_204_NO_CONTENT:
      if (myReturnVoid) {
        return null;
      }
      List<String> locationHeaders = theHeaders.get("location");
      MethodOutcome retVal = new MethodOutcome();
      if (locationHeaders != null && locationHeaders.size() > 0) {
        String locationHeader = locationHeaders.get(0);
        parseContentLocation(retVal, locationHeader);
      }
      if (theResponseStatusCode != Constants.STATUS_HTTP_204_NO_CONTENT) {
        EncodingUtil ct = EncodingUtil.forContentType(theResponseMimeType);
        if (ct != null) {
          PushbackReader reader = new PushbackReader(theResponseReader);

          try {
            int firstByte = reader.read();
            if (firstByte == -1) {
              ourLog.debug("No content in response, not going to read");
              reader = null;
            } else {
              reader.unread(firstByte);
            }
          } catch (IOException e) {
            ourLog.debug("No content in response, not going to read", e);
            reader = null;
          }

          if (reader != null) {
            IParser parser = ct.newParser(getContext());
            OperationOutcome outcome = parser.parseResource(OperationOutcome.class, reader);
            retVal.setOperationOutcome(outcome);
          }

        } else {
          ourLog.debug("Ignoring response content of type: {}", theResponseMimeType);
        }
View Full Code Here

  public void invokeServer(RestfulServer theServer, Request theRequest, HttpServletResponse theResponse) throws BaseServerResponseException, IOException {
    Object[] params = new Object[myParameters.size()];

    addParametersForServerRequest(theRequest, params);

    MethodOutcome response;
    try {
      response = (MethodOutcome) this.getMethod().invoke(theRequest.getResourceProvider(), params);
    } catch (IllegalAccessException e) {
      throw new InternalErrorException(e);
    } catch (IllegalArgumentException e) {
      throw new InternalErrorException(e);
    } catch (InvocationTargetException e) {
      throw new InternalErrorException(e);
    }

    if (response == null) {
      if (myReturnVoid == false) {
        throw new ConfigurationException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName() + " returned null");
      } else {
        theResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT);
      }
    } else if (!myReturnVoid) {
      if (response.isCreated()) {
        theResponse.setStatus(Constants.STATUS_HTTP_201_CREATED);
        StringBuilder b = new StringBuilder();
        b.append(theRequest.getFhirServerBase());
        b.append('/');
        b.append(getResourceName());
        b.append('/');
        b.append(response.getId().getValue());
        if (response.getVersionId() != null && response.getVersionId().isEmpty() == false) {
          b.append("/_history/");
          b.append(response.getVersionId().getValue());
        }
        theResponse.addHeader("Location", b.toString());
      } else {
        theResponse.setStatus(Constants.STATUS_HTTP_200_OK);
      }
    } else {
      theResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT);
    }

    theServer.addHapiHeader(theResponse);

    Writer writer = theResponse.getWriter();
    try {
      if (response != null) {
        OperationOutcome outcome = new OperationOutcome();
        if (response.getOperationOutcome() != null && response.getOperationOutcome().getIssue() != null) {
          outcome.getIssue().addAll(response.getOperationOutcome().getIssue());
        }
        EncodingUtil encoding = BaseMethodBinding.determineResponseEncoding(theRequest.getServletRequest(), theRequest.getParameters());
        theResponse.setContentType(encoding.getResourceContentType());
        IParser parser = encoding.newParser(getContext());
        parser.encodeResourceToWriter(outcome, writer);
View Full Code Here

      params[i] = param.translateQueryParametersIntoServerArgument(theRequest.getParameters(), resource);
    }

    addParametersForServerRequest(theRequest, params);

    MethodOutcome response;
    try {
      response = (MethodOutcome) this.getMethod().invoke(theRequest.getResourceProvider(), params);
    } catch (IllegalAccessException e) {
      throw new InternalErrorException(e);
    } catch (IllegalArgumentException e) {
      throw new InternalErrorException(e);
    } catch (InvocationTargetException e) {
      throw new InternalErrorException(e);
    }

    if (response == null) {
      if (isReturnVoid() == false) {
        throw new ConfigurationException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName() + " returned null");
      }
    } else if (!isReturnVoid()) {
      if (response.isCreated()) {
        theResponse.setStatus(Constants.STATUS_HTTP_201_CREATED);
        StringBuilder b = new StringBuilder();
        b.append(theRequest.getFhirServerBase());
        b.append('/');
        b.append(getResourceName());
        b.append('/');
        b.append(response.getId().getValue());
        if (response.getVersionId() != null && response.getVersionId().isEmpty() == false) {
          b.append("/_history/");
          b.append(response.getVersionId().getValue());
        }
        theResponse.addHeader("Location", b.toString());
      } else {
        theResponse.setStatus(Constants.STATUS_HTTP_200_OK);
      }
View Full Code Here

     * supposed to include the version in the Content-Location header, but
     * we allow it in the PUT URL as well..
     */
    String locationHeader = theRequest.getServletRequest().getHeader(Constants.HEADER_CONTENT_LOCATION);
    if (isNotBlank(locationHeader)) {
      MethodOutcome mo = new MethodOutcome();
      parseContentLocation(mo, locationHeader);
      if (mo.getId() == null || mo.getId().isEmpty()) {
        throw new InvalidRequestException("Invalid Content-Location header for resource " + getResourceName()+ ": " + locationHeader);
      }
      if (mo.getVersionId() != null && mo.getVersionId().isEmpty() == false) {
        theRequest.setVersion(mo.getVersionId());
      }
    }

    // TODO: we should send an HTTP 412 automatically if the server
    // only has a method which requires a version ID, and no
View Full Code Here

    case Constants.STATUS_HTTP_201_CREATED:
    case Constants.STATUS_HTTP_204_NO_CONTENT:
      if (myReturnVoid) {
        return null;
      }
      MethodOutcome retVal = MethodUtil.process2xxResponse(getContext(), getResourceName(), theResponseStatusCode, theResponseMimeType, theResponseReader, theHeaders);
      return retVal;
    default:
      throw processNon2xxResponseAndReturnExceptionToThrow(theResponseStatusCode, theResponseMimeType, theResponseReader);
    }
View Full Code Here

    }

    Object[] params = createParametersForServerRequest(theRequest, resource);
    addParametersForServerRequest(theRequest, params);

    MethodOutcome response;
    try {
      response = (MethodOutcome) invokeServerMethod(params);
    } catch (InternalErrorException e) {
      ourLog.error("Internal error during method invocation", e);
      EncodingEnum encoding = RestfulServer.determineResponseEncoding(theRequest.getServletRequest());
      streamOperationOutcome(e, theServer, encoding, theResponse, theRequest);
      return;
    } catch (BaseServerResponseException e) {
      ourLog.info("Exception during method invocation: " + e.getMessage());
      EncodingEnum encoding = RestfulServer.determineResponseEncoding(theRequest.getServletRequest());
      streamOperationOutcome(e, theServer, encoding, theResponse, theRequest);
      return;
    }

    if (response != null && response.getId() != null && response.getId().hasResourceType()) {
      if (getContext().getResourceDefinition(response.getId().getResourceType()) == null) {
        throw new InternalErrorException("Server method returned invalid resource ID: " + response.getId().getValue());
      }
    }
   
    if (getResourceOperationType() == RestfulOperationTypeEnum.CREATE) {
      if (response == null) {
        throw new InternalErrorException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName() + " returned null, which is not allowed for create operation");
      }
      theResponse.setStatus(Constants.STATUS_HTTP_201_CREATED);
      addLocationHeader(theRequest, theResponse, response);
    } else if (response == null) {
      if (isReturnVoid() == false) {
        throw new InternalErrorException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName() + " returned null");
      }
      theResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT);
    } else {
      if (response.getOperationOutcome() == null) {
        theResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT);
      } else {
        theResponse.setStatus(Constants.STATUS_HTTP_200_OK);
      }
      if (getResourceOperationType() == RestfulOperationTypeEnum.UPDATE) {
        addLocationHeader(theRequest, theResponse, response);
      }

    }

    theServer.addHeadersToResponse(theResponse);

    if (response != null && response.getOperationOutcome() != null) {
      EncodingEnum encoding = RestfulServer.determineResponseEncoding(theRequest.getServletRequest());
      theResponse.setContentType(encoding.getResourceContentType());
      Writer writer = theResponse.getWriter();
      IParser parser = encoding.newParser(getContext());
      parser.setPrettyPrint(RestfulServer.prettyPrintResponse(theRequest));
      try {
        parser.encodeResourceToWriter(response.getOperationOutcome(), writer);
      } finally {
        writer.close();
      }
    } else {
      theResponse.setContentType(Constants.CT_TEXT);
View Full Code Here

        theRequest.setId(id);
      }
    }
   
    if (isNotBlank(locationHeader)) {
      MethodOutcome mo = new MethodOutcome();
      parseContentLocation(mo, getResourceName(), locationHeader);
      if (mo.getId() == null || mo.getId().isEmpty()) {
        throw new InvalidRequestException("Invalid Content-Location header for resource " + getResourceName() + ": " + locationHeader);
      }
      if (mo.getVersionId() != null && mo.getVersionId().isEmpty() == false) {
        theRequest.setVersion(mo.getVersionId());
      }
    }

    theParams[myIdParameterIndex] = theRequest.getId();
    if (myVersionIdParameterIndex != null) {
View Full Code Here

      myResourceName = theResourceName;
    }

    @Override
    public MethodOutcome invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
      MethodOutcome response = MethodUtil.process2xxResponse(myContext, myResourceName, theResponseStatusCode, theResponseMimeType, theResponseReader, theHeaders);
      return response;
    }
View Full Code Here

    List<String> clh = theHeaders.get(Constants.HEADER_CONTENT_LOCATION_LC);
    if (clh != null) {
      locationHeaders.addAll(clh);
    }

    MethodOutcome retVal = new MethodOutcome();
    if (locationHeaders != null && locationHeaders.size() > 0) {
      String locationHeader = locationHeaders.get(0);
      BaseOutcomeReturningMethodBinding.parseContentLocation(retVal, theResourceName, locationHeader);
    }
    if (theResponseStatusCode != Constants.STATUS_HTTP_204_NO_CONTENT) {
      EncodingEnum ct = EncodingEnum.forContentType(theResponseMimeType);
      if (ct != null) {
        PushbackReader reader = new PushbackReader(theResponseReader);

        try {
          int firstByte = reader.read();
          if (firstByte == -1) {
            BaseOutcomeReturningMethodBinding.ourLog.debug("No content in response, not going to read");
            reader = null;
          } else {
            reader.unread(firstByte);
          }
        } catch (IOException e) {
          BaseOutcomeReturningMethodBinding.ourLog.debug("No content in response, not going to read", e);
          reader = null;
        }

        if (reader != null) {
          IParser parser = ct.newParser(theContext);
          IResource outcome = parser.parseResource(reader);
          if (outcome instanceof OperationOutcome) {
            retVal.setOperationOutcome((OperationOutcome) outcome);
          }
        }

      } else {
        BaseOutcomeReturningMethodBinding.ourLog.debug("Ignoring response content of type: {}", theResponseMimeType);
View Full Code Here

TOP

Related Classes of ca.uhn.fhir.rest.api.MethodOutcome

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.