Package ca.uhn.fhir.rest.server

Examples of ca.uhn.fhir.rest.server.EncodingEnum


  private final class OperationOutcomeResponseHandler implements IClientResponseHandler<OperationOutcome> {

    @Override
    public OperationOutcome invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
      EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
      if (respType == null) {
        return null;
      }
      IParser parser = respType.newParser(myContext);
      OperationOutcome retVal;
      try {
        retVal = parser.parseResource(OperationOutcome.class, theResponseReader);
      } catch (DataFormatException e) {
        ourLog.warn("Failed to parse OperationOutcome response", e);
View Full Code Here


   
      }

  @Override
  protected Object parseRequestObject(Request theRequest) throws IOException {
    EncodingEnum encoding = RestfulServer.determineResponseEncoding(theRequest.getServletRequest());
    IParser parser = encoding.newParser(getContext());
    Bundle bundle = parser.parseBundle(theRequest.getServletRequest().getReader());
    return bundle;
  }
View Full Code Here

      myId = theId;
    }

    @Override
    public T invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
      EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
      if (respType == null) {
        throw NonFhirResponseException.newInstance(theResponseStatusCode, theResponseMimeType, theResponseReader);
      }
      IParser parser = respType.newParser(myContext);
      T retVal = parser.parseResource(myType, theResponseReader);

      if (myId != null) {
        retVal.setId(myId);
      }
View Full Code Here

  private final class TagListResponseHandler implements IClientResponseHandler<TagList> {

    @Override
    public TagList invokeClient(String theResponseMimeType, Reader theResponseReader, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws IOException, BaseServerResponseException {
      EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType);
      if (respType == null) {
        throw NonFhirResponseException.newInstance(theResponseStatusCode, theResponseMimeType, theResponseReader);
      }
      IParser parser = respType.newParser(myContext);
      return parser.parseTagList(theResponseReader);
    }
View Full Code Here

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

      }
      if (thePrettyPrint == Boolean.TRUE) {
        params.put(Constants.PARAM_PRETTY, Collections.singletonList(Constants.PARAM_PRETTY_VALUE_TRUE));
      }

      EncodingEnum encoding = getEncoding();
      if (theEncoding != null) {
        encoding=theEncoding;
      }
     
      httpRequest = clientInvocation.asHttpRequest(myUrlBase, params, encoding);

      if (theLogRequestAndResponse) {
        ourLog.info("Client invoking: {}", httpRequest);
        if (httpRequest instanceof HttpEntityEnclosingRequest) {
          HttpEntity entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
          if (entity.isRepeatable()) {
            String content = IOUtils.toString(entity.getContent());
            ourLog.info("Client request body: {}", content);
          }
        }
      }

      for (IClientInterceptor nextInterceptor : myInterceptors) {
        nextInterceptor.interceptRequest(httpRequest);
      }

      response = myClient.execute(httpRequest);

      for (IClientInterceptor nextInterceptor : myInterceptors) {
        nextInterceptor.interceptResponse(response);
      }

    } catch (DataFormatException e) {
      throw new FhirClientConnectionException(e);
    } catch (IOException e) {
      throw new FhirClientConnectionException(e);
    }

    try {
      ContentType ct = ContentType.get(response.getEntity());
      String mimeType = ct != null ? ct.getMimeType() : null;

      Map<String, List<String>> headers = new HashMap<String, List<String>>();
      if (response.getAllHeaders() != null) {
        for (Header next : response.getAllHeaders()) {
          String name = next.getName().toLowerCase();
          List<String> list = headers.get(name);
          if (list == null) {
            list = new ArrayList<String>();
            headers.put(name, list);
          }
          list.add(next.getValue());
        }
      }

      if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) {
        String body = null;
        Reader reader = null;
        try {
          reader = createReaderFromResponse(response);
          body = IOUtils.toString(reader);
        } catch (Exception e) {
          ourLog.debug("Failed to read input stream", e);
        } finally {
          IOUtils.closeQuietly(reader);
        }

        String message = "HTTP " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase();
        OperationOutcome oo=null;
        if (Constants.CT_TEXT.equals(mimeType)) {
          message = message + ": " + body;
        } else {
          EncodingEnum enc = EncodingEnum.forContentType(mimeType);
          if (enc != null) {
            IParser p = enc.newParser(theContext);
            try {
              oo = p.parseResource(OperationOutcome.class, body);
              if (oo.getIssueFirstRep().getDetails().isEmpty()==false) {
                message = message + ": " + oo.getIssueFirstRep().getDetails().getValue();
              }
View Full Code Here

    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, servletResponse, theRequest);
      return;
    } catch (BaseServerResponseException e) {
      ourLog.info("Exception during method invocation: " + e.getMessage());
      EncodingEnum encoding = RestfulServer.determineResponseEncoding(theRequest.getServletRequest());
      streamOperationOutcome(e, theServer, encoding, servletResponse, 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());
      }
    }
   
    OperationOutcome outcome = response != null ? response.getOperationOutcome():null;
    for (int i = theServer.getInterceptors().size() - 1; i >= 0; i--) {
      IServerInterceptor next = theServer.getInterceptors().get(i);
      boolean continueProcessing = next.outgoingResponse(theRequest, outcome, theRequest.getServletRequest(), theRequest.getServletResponse());
      if (!continueProcessing) {
        return;
      }
    }
   
    switch (getResourceOperationType()) {
    case CREATE:
      if (response == null) {
        throw new InternalErrorException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName()
            + " returned null, which is not allowed for create operation");
      }
      if (response.getCreated() == null || Boolean.TRUE.equals(response.getCreated())) {
        servletResponse.setStatus(Constants.STATUS_HTTP_201_CREATED);
      } else {
        servletResponse.setStatus(Constants.STATUS_HTTP_200_OK);
      }
      addLocationHeader(theRequest, servletResponse, response);
      break;

    case UPDATE:
      if (response.getCreated() == null || Boolean.FALSE.equals(response.getCreated())) {
        servletResponse.setStatus(Constants.STATUS_HTTP_200_OK);
      } else {
        servletResponse.setStatus(Constants.STATUS_HTTP_201_CREATED);
      }
      addLocationHeader(theRequest, servletResponse, response);
      break;

    case VALIDATE:
    case DELETE:
    default:
      if (response == null) {
        if (isReturnVoid() == false) {
          throw new InternalErrorException("Method " + getMethod().getName() + " in type " + getMethod().getDeclaringClass().getCanonicalName() + " returned null");
        }
        servletResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT);
      } else {
        if (response.getOperationOutcome() == null) {
          servletResponse.setStatus(Constants.STATUS_HTTP_204_NO_CONTENT);
        } else {
          servletResponse.setStatus(Constants.STATUS_HTTP_200_OK);
        }
      }

    }

    theServer.addHeadersToResponse(servletResponse);

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

  /**
   * @throws IOException
   */
  protected IResource parseIncomingServerResource(Request theRequest) throws IOException {
    EncodingEnum encoding = RestfulServer.determineRequestEncoding(theRequest);
    IParser parser = encoding.newParser(getContext());
    IResource resource = parser.parseResource(theRequest.getServletRequest().getReader());
    return resource;
  }
View Full Code Here

      return retVal;
    }

    IParser parser;
    String contentType;
    EncodingEnum encoding = null;
    encoding = theEncoding;

    if (encoding == EncodingEnum.JSON) {
      parser = myContext.newJsonParser();
    } else {
      encoding = EncodingEnum.XML;
      parser = myContext.newXmlParser();
    }

    AbstractHttpEntity entity;
    if (myParams != null) {
      List<NameValuePair> parameters = new ArrayList<NameValuePair>();
      for (Entry<String, List<String>> nextParam : myParams.entrySet()) {
        parameters.add(new BasicNameValuePair(nextParam.getKey(), StringUtils.join(nextParam.getValue(), ',')));
      }
      try {
        entity = new UrlEncodedFormEntity(parameters, "UTF-8");
      } catch (UnsupportedEncodingException e) {
        throw new InternalErrorException("Server does not support UTF-8 (should not happen)", e);
      }
    } else {
      String contents;
      if (myTagList != null) {
        contents = parser.encodeTagListToString(myTagList);
        contentType = encoding.getResourceContentType();
      } else if (myBundle != null) {
        contents = parser.encodeBundleToString(myBundle);
        contentType = encoding.getBundleContentType();
      } else if (myResources != null) {
        Bundle bundle = RestfulServer.createBundleFromResourceList(myContext, "", myResources, "", "", myResources.size());
        contents = parser.encodeBundleToString(bundle);
        contentType = encoding.getBundleContentType();
      } else if (myContents != null) {
        contents = myContents;
        if (myContentsIsBundle) {
          contentType = encoding.getBundleContentType();
        } else {
          contentType = encoding.getResourceContentType();
        }
      } else {
        contents = parser.encodeResourceToString(myResource);
        contentType = encoding.getResourceContentType();
      }
      entity = new StringEntity(contents, ContentType.create(contentType, "UTF-8"));
    }

    HttpRequestBase retVal = createRequest(url, entity);
View Full Code Here

          StringWriter requestCapture = new StringWriter();
          lastEE.writeTo(new WriterOutputStream(requestCapture, "UTF-8"));
          requestBody = requestCapture.toString();
          ContentType ct = ContentType.get(lastEE);
          String mimeType = ct.getMimeType();
          EncodingEnum ctEnum = EncodingEnum.forContentType(mimeType);
          if (ctEnum == null) {
            requestSyntaxHighlighterClass = "brush: plain";
          } else {
            switch (ctEnum) {
            case JSON:
              requestSyntaxHighlighterClass = "brush: jscript";
              break;
            case XML:
            default:
              requestSyntaxHighlighterClass = "brush: xml";
              break;
            }
          }
        }
      }
      requestUrl = lastRequest.getURI().toASCIIString();
      action = client.getLastRequest().getMethod();
      resultStatus = client.getLastResponse().getStatusLine().toString();
      resultBody = client.getLastResponseBody();

      HttpResponse lastResponse = client.getLastResponse();
      ContentType ct = ContentType.get(lastResponse.getEntity());
      String mimeType = ct != null ? ct.getMimeType() : null;
      EncodingEnum ctEnum = EncodingEnum.forContentType(mimeType);
      String narrativeString = "";

      if (ctEnum == null) {
        resultSyntaxHighlighterClass = "brush: plain";
      } else {
View Full Code Here

TOP

Related Classes of ca.uhn.fhir.rest.server.EncodingEnum

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.