Package ca.uhn.fhir.parser

Examples of ca.uhn.fhir.parser.IParser


  public abstract ReturnTypeEnum getReturnType();

  @Override
  public Object invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException {
    IParser parser = createAppropriateParserForParsingResponse(theResponseMimeType, theResponseReader, theResponseStatusCode);

    switch (getReturnType()) {
    case BUNDLE: {
      Bundle bundle;
      if (myResourceType != null) {
        bundle = parser.parseBundle(myResourceType, theResponseReader);
      } else {
        bundle = parser.parseBundle(theResponseReader);
      }
      switch (getMethodReturnType()) {
      case BUNDLE:
        return bundle;
      case LIST_OF_RESOURCES:
        return bundle.toListOfResources();
      case RESOURCE:
        List<IResource> list = bundle.toListOfResources();
        if (list.size() == 0) {
          return null;
        } else if (list.size() == 1) {
          return list.get(0);
        } else {
          throw new InvalidResponseException(theResponseStatusCode, "FHIR server call returned a bundle with multiple resources, but this method is only able to returns one.");
        }
      }
      break;
    }
    case RESOURCE: {
      IResource resource;
      if (myResourceType != null) {
        resource = parser.parseResource(myResourceType, theResponseReader);
      } else {
        resource = parser.parseResource(theResponseReader);
      }

      List<String> lmHeaders = theHeaders.get(Constants.HEADER_LAST_MODIFIED_LOWERCASE);
      if (lmHeaders != null && lmHeaders.size() > 0 && StringUtils.isNotBlank(lmHeaders.get(0))) {
        String headerValue = lmHeaders.get(0);
View Full Code Here


    }
    throw new InternalErrorException("Found an object of type '" + retValObj.getClass().getCanonicalName() + "' in resource metadata for key " + theKey.name() + " - Expected " + TagList.class.getCanonicalName());
  }

  private IParser getNewParser(EncodingEnum theResponseEncoding, boolean thePrettyPrint, NarrativeModeEnum theNarrativeMode) {
    IParser parser;
    switch (theResponseEncoding) {
    case JSON:
      parser = getContext().newJsonParser();
      break;
    case XML:
    default:
      parser = getContext().newXmlParser();
      break;
    }
    return parser.setPrettyPrint(thePrettyPrint).setSuppressNarratives(theNarrativeMode == NarrativeModeEnum.SUPPRESS);
  }
View Full Code Here

   
    HttpGet get = new HttpGet(resourceUrl);
    HttpResponse response = httpClient.execute(get);
    try {
      // TODO: choose appropriate parser based on response CT
      IParser parser = context.newXmlParser();

      Reader responseReader = BaseClient.createReaderFromResponse(response);
      myResource = parser.parseResource(responseReader);

    } finally {
      if (response instanceof CloseableHttpResponse) {
        ((CloseableHttpResponse) response).close();
      }
View Full Code Here

  }

  @Override
  public void invokeServer(RestfulServer theServer, Request theRequest, HttpServletResponse theResponse) throws BaseServerResponseException, IOException {
    EncodingEnum encoding = determineResponseEncoding(theRequest);
    IParser parser = encoding.newParser(getContext());
    IResource resource;
    if (requestContainsResource()) {
      resource = parser.parseResource(theRequest.getInputReader());
      TagList tagList = new TagList();
      for (Enumeration<String> enumeration = theRequest.getServletRequest().getHeaders(Constants.HEADER_CATEGORY); enumeration.hasMoreElements();) {
        String nextTagComplete = enumeration.nextElement();
        StringBuilder next = new StringBuilder(nextTagComplete);
        parseTagValue(tagList, nextTagComplete, next);
      }
      if (tagList.isEmpty() == false) {
        resource.getResourceMetadata().put(ResourceMetadataKeyEnum.TAG_LIST, tagList);
      }
    } else {
      resource = null;
    }

    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);
      streamOperationOutcome(e, theServer, encoding, theResponse, theRequest);
      return;
    } catch (BaseServerResponseException e) {
      ourLog.info("Exception during method invocation: " + e.getMessage());
      streamOperationOutcome(e, theServer, encoding, theResponse, theRequest);
      return;
    }

    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) {
      theResponse.setContentType(encoding.getResourceContentType());
      Writer writer = theResponse.getWriter();
      parser.setPrettyPrint(prettyPrintResponse(theRequest));
      try {
        parser.encodeResourceToWriter(response.getOperationOutcome(), writer);
      } finally {
        writer.close();
      }
    } else {
      theResponse.setContentType(Constants.CT_TEXT);
View Full Code Here

    theServer.addHeadersToResponse(theResponse);

    if (theE.getOperationOutcome() != null) {
      theResponse.setContentType(theEncoding.getResourceContentType());
      IParser parser = theEncoding.newParser(theServer.getFhirContext());
      parser.setPrettyPrint(prettyPrintResponse(theRequest));
      Writer writer = theResponse.getWriter();
      try {
        parser.encodeResourceToWriter(theE.getOperationOutcome(), writer);
      } finally {
        writer.close();
      }
    } else {
      theResponse.setContentType(Constants.CT_TEXT);
View Full Code Here

          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);
          }
        }
View Full Code Here

  }

  @Override
  public TagList invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
    if (theResponseStatusCode == Constants.STATUS_HTTP_200_OK) {
      IParser parser = createAppropriateParserForParsingResponse(theResponseMimeType, theResponseReader, theResponseStatusCode);
      TagList retVal = parser.parseTagList(theResponseReader);
      return retVal;
    } else {
      throw processNon2xxResponseAndReturnExceptionToThrow(theResponseStatusCode, theResponseMimeType, theResponseReader);
    }
View Full Code Here

    theResponse.setStatus(Constants.STATUS_HTTP_200_OK);
    theResponse.setCharacterEncoding(Constants.CHARSET_UTF_8);

    theServer.addHeadersToResponse(theResponse);

    IParser parser = responseEncoding.newParser(getContext());
    parser.setPrettyPrint(prettyPrintResponse(theRequest));
    PrintWriter writer = theResponse.getWriter();
    try {
      parser.encodeTagListToWriter(resp, writer);
    } finally {
      writer.close();
    }
  }
View Full Code Here

            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);
        }
      }
      return retVal;
    case Constants.STATUS_HTTP_400_BAD_REQUEST:
      throw new InvalidRequestException("Server responded with: " + IOUtils.toString(theResponseReader));
    case Constants.STATUS_HTTP_404_NOT_FOUND:
      throw new ResourceNotFoundException("Server responded with: " + IOUtils.toString(theResponseReader));
    case Constants.STATUS_HTTP_405_METHOD_NOT_ALLOWED:
      throw new MethodNotAllowedException("Server responded with: " + IOUtils.toString(theResponseReader));
    case Constants.STATUS_HTTP_409_CONFLICT:
      throw new ResourceVersionConflictException("Server responded with: " + IOUtils.toString(theResponseReader));
    case Constants.STATUS_HTTP_412_PRECONDITION_FAILED:
      throw new ResourceVersionNotSpecifiedException("Server responded with: " + IOUtils.toString(theResponseReader));
    case Constants.STATUS_HTTP_422_UNPROCESSABLE_ENTITY:
      IParser parser = createAppropriateParser(theResponseMimeType, theResponseReader, theResponseStatusCode);
      OperationOutcome operationOutcome = parser.parseResource(OperationOutcome.class, theResponseReader);
      throw new UnprocessableEntityException(operationOutcome);
    default:
      throw new UnclassifiedServerFailureException(theResponseStatusCode, IOUtils.toString(theResponseReader));
    }
View Full Code Here

        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);
      }
    } finally {
      writer.close();
    }
    // getMethod().in
View Full Code Here

TOP

Related Classes of ca.uhn.fhir.parser.IParser

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.