@Override
  public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) {
    Set<RequestTypeEnum> allowableRequestTypes = provideAllowableRequestTypes();
    RequestTypeEnum requestType = theRequest.getRequestType();
    if (!allowableRequestTypes.contains(requestType)) {
      return false;
    }
    if (!getResourceName().equals(theRequest.getResourceName())) {
      return false;
    }
    if (getMatchingOperation() == null && StringUtils.isNotBlank(theRequest.getOperation())) {
      return false;
    }
    if (getMatchingOperation() != null
        && !getMatchingOperation().equals(theRequest.getOperation())) {
      return false;
    }

    /*
     * Note: Technically this will match an update (PUT) method even if
     * there is no ID in the URL - We allow this here because there is no
     * better match for that, and this allows the update/PUT method to give
     * a helpful error if the client has forgotten to include the
     * ID in the URL.
     *
     * It's also needed for conditional update..
     */

    return true;
  }
 @Override
 public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) {
   if (theRequest.getRequestType() != RequestTypeEnum.POST) {
     return false;
   }
   if (isNotBlank(theRequest.getOperation())) {
     return false;
   }
   if (isNotBlank(theRequest.getResourceName())) {
     return false;
   }
   return true;
 }
  @Override
  public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) {
    if (theRequest.getRequestType() == RequestTypeEnum.OPTIONS) {
      if (theRequest.getOperation() == null && theRequest.getResourceName() == null) {
        return true;
      }
    }

    if (theRequest.getResourceName() != null) {
      return false;
    }

    if ("metadata".equals(theRequest.getOperation())) {
      if (theRequest.getRequestType() == RequestTypeEnum.GET) {
        return true;
      } else {
        throw new MethodNotAllowedException(
            "/metadata request must use HTTP GET", RequestTypeEnum.GET);
      }
    }

    return false;
  }
 @Override
 protected void addParametersForServerRequest(RequestDetails theRequest, Object[] theParams) {
   theParams[getIdParameterIndex()] = theRequest.getId();
 }
  private Object returnResponse(
      IRestfulServer<?> theServer,
      RequestDetails theRequest,
      MethodOutcome response,
      IBaseResource originalOutcome,
      IBaseResource resource)
      throws IOException {
    boolean allowPrefer = false;
    int operationStatus = getOperationStatus(response);
    IBaseResource outcome = originalOutcome;

    if (ourOperationsWhichAllowPreferHeader.contains(getRestOperationType())) {
      allowPrefer = true;
    }

    if (resource != null && allowPrefer) {
      String prefer = theRequest.getHeader(Constants.HEADER_PREFER);
      PreferReturnEnum preferReturn = RestfulServerUtils.parsePreferHeader(prefer);
      if (preferReturn != null) {
        if (preferReturn == PreferReturnEnum.REPRESENTATION) {
          outcome = resource;
        }
      }
    }

    for (int i = theServer.getInterceptors().size() - 1; i >= 0; i--) {
      IServerInterceptor next = theServer.getInterceptors().get(i);
      boolean continueProcessing = next.outgoingResponse(theRequest, outcome);
      if (!continueProcessing) {
        return null;
      }
    }

    IRestfulResponse restfulResponse = theRequest.getResponse();

    if (response != null) {
      if (response.getResource() != null) {
        restfulResponse.setOperationResourceLastUpdated(
            RestfulServerUtils.extractLastUpdatedFromResource(response.getResource()));
      }

      IIdType responseId = response.getId();
      if (responseId != null && responseId.getResourceType() == null && responseId.hasIdPart()) {
        responseId = responseId.withResourceType(getResourceName());
      }

      if (responseId != null) {
        String serverBase = theRequest.getFhirServerBase();
        responseId =
            RestfulServerUtils.fullyQualifyResourceIdOrReturnNull(
                theServer, resource, serverBase, responseId);
        restfulResponse.setOperationResourceId(responseId);
      }
    }

    boolean prettyPrint = RestfulServerUtils.prettyPrintResponse(theServer, theRequest);
    Set<SummaryEnum> summaryMode = Collections.emptySet();

    return restfulResponse.streamResponseAsResource(
        outcome, prettyPrint, summaryMode, operationStatus, null, theRequest.isRespondGzip(), true);
    //		return theRequest.getResponse().returnResponse(ParseAction.create(outcome), operationStatus,
    // allowPrefer, response, getResourceName());
  }
示例#6
0
  @Override
  public Request prepareRequest(final Context context, final RequestDetails details) {

    final com.squareup.okhttp.Request.Builder builder = new com.squareup.okhttp.Request.Builder();

    final List<PostField> postFields = details.getPostFields();

    if (postFields != null) {
      builder.post(
          RequestBody.create(
              MediaType.parse("application/x-www-form-urlencoded"),
              PostField.encodeList(postFields)));

    } else {
      builder.get();
    }

    builder.url(details.getUrl().toString());
    builder.cacheControl(CacheControl.FORCE_NETWORK);

    final AtomicReference<Call> callRef = new AtomicReference<>();

    return new Request() {

      public void executeInThisThread(final Listener listener) {

        final Call call = mClient.newCall(builder.build());
        callRef.set(call);

        try {

          final Response response;

          try {
            response = call.execute();
          } catch (IOException e) {
            listener.onError(CacheRequest.REQUEST_FAILURE_CONNECTION, e, null);
            return;
          }

          final int status = response.code();

          if (status == 200 || status == 202) {

            final ResponseBody body = response.body();
            final InputStream bodyStream;
            final Long bodyBytes;

            if (body != null) {
              bodyStream = body.byteStream();
              bodyBytes = body.contentLength();

            } else {
              // TODO error
              bodyStream = null;
              bodyBytes = null;
            }

            final String contentType = response.header("Content-Type");

            listener.onSuccess(contentType, bodyBytes, bodyStream);

          } else {
            listener.onError(CacheRequest.REQUEST_FAILURE_REQUEST, null, status);
          }

        } catch (Throwable t) {
          listener.onError(CacheRequest.REQUEST_FAILURE_CONNECTION, t, null);
        }
      }

      @Override
      public void cancel() {
        final Call call = callRef.getAndSet(null);
        if (call != null) {
          call.cancel();
        }
      }

      @Override
      public void addHeader(final String name, final String value) {
        builder.addHeader(name, value);
      }
    };
  }