Java Code Examples for org.apache.olingo.commons.api.http.HttpMethod#POST

The following examples show how to use org.apache.olingo.commons.api.http.HttpMethod#POST . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatchReferenceRewriter.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private String getODataPath(final ODataRequest request, final ODataResponse response)
    throws BatchDeserializerException {
  String resourceUri = null;

  if (request.getMethod() == HttpMethod.POST) {
    // Create entity
    // The URI of the new resource will be generated by the server and published in the location header
    final String locationHeader = response.getHeader(HttpHeader.LOCATION);
    resourceUri = locationHeader == null ? null : parseODataPath(locationHeader, request.getRawBaseUri());
  } else {
    // Update, Upsert (PUT, PATCH, Delete)
    // These methods still addresses a given resource, so we use the URI given by the request
    resourceUri = request.getRawODataPath();
  }

  return resourceUri;
}
 
Example 2
Source File: CUDRequestFactoryImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataPropertyUpdateRequest getPropertyComplexValueUpdateRequest(
    final URI targetURI, final UpdateType type, final ClientProperty property) {

  if (!property.hasComplexValue()) {
    throw new IllegalArgumentException("A complex value is required");
  }

  final ODataPropertyUpdateRequest req;

  if (client.getConfiguration().isUseXHTTPMethod()) {
    req = new ODataPropertyUpdateRequestImpl(client, HttpMethod.POST, targetURI, property);
    req.setXHTTPMethod(type.getMethod().name());
  } else {
    req = new ODataPropertyUpdateRequestImpl(client, type.getMethod(), targetURI, property);
  }

  return req;
}
 
Example 3
Source File: AbstractODataInvokeRequest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc }
 */
@Override
public ODataInvokeResponse<T> execute() {
  final InputStream input = getPayload();

  if (!this.parameters.isEmpty()) {
    if (this.method == HttpMethod.GET) {
      ((HttpRequestBase) this.request).setURI(
          URIUtils.buildFunctionInvokeURI(this.uri, parameters));
    } else if (this.method == HttpMethod.POST) {
      ((HttpPost) request).setEntity(URIUtils.buildInputStreamEntity(odataClient, input));

      setContentType(getActualFormat(getPOSTParameterFormat()));
    }
  }

  try {
    return new ODataInvokeResponseImpl(odataClient, httpClient, doExecute());
  } finally {
    IOUtils.closeQuietly(input);
  }
}
 
Example 4
Source File: MockedBatchHandlerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private ODataResponse buildResponse(final ODataRequest request) {
  final ODataResponse oDataResponse = new ODataResponse();

  if (request.getMethod() == HttpMethod.POST) {
    oDataResponse.setStatusCode(HttpStatusCode.CREATED.getStatusCode());
    oDataResponse.setHeader(HttpHeader.LOCATION, createResourceUri(request));
  } else {
    oDataResponse.setStatusCode(HttpStatusCode.OK.getStatusCode());
  }

  final String contentId = request.getHeader(HttpHeader.CONTENT_ID);
  if (contentId != null) {
    oDataResponse.setHeader(HttpHeader.CONTENT_ID, contentId);
  }

  return oDataResponse;
}
 
Example 5
Source File: CUDRequestFactoryImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataPropertyUpdateRequest getPropertyPrimitiveValueUpdateRequest(
    final URI targetURI, final ClientProperty property) {

  if (!property.hasPrimitiveValue()) {
    throw new IllegalArgumentException("A primitive value is required");
  }

  final ODataPropertyUpdateRequest req;

  if (client.getConfiguration().isUseXHTTPMethod()) {
    req = new ODataPropertyUpdateRequestImpl(client, HttpMethod.POST, targetURI, property);
    req.setXHTTPMethod(HttpMethod.PUT.name());
  } else {
    req = new ODataPropertyUpdateRequestImpl(client, HttpMethod.PUT, targetURI, property);
  }

  return req;
}
 
Example 6
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void missingContentType() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + ";boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      // + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_HTTP + CRLF
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESAllPrim" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + "--" + BOUNDARY + "--";

  parseInvalidBatchBody(batch, BatchDeserializerException.MessageKeys.MISSING_CONTENT_TYPE);
}
 
Example 7
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void nestedChangeset() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + ";boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + ";boundary=changeset_f980-1cb6-94dd2" + CRLF
      + CRLF
      + "--changeset_f980-1cb6-94dd2" + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESAllPrim" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + HttpHeader.CONTENT_ID + ": 2" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  parseInvalidBatchBody(batch, BatchDeserializerException.MessageKeys.UNEXPECTED_CONTENT_TYPE);
}
 
Example 8
Source File: CUDRequestFactoryImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public <E extends ClientEntity> ODataEntityUpdateRequest<E> getEntityUpdateRequest(
    final UpdateType type, final E entity) {

  if (entity.getEditLink() == null) {
    throw new IllegalArgumentException("No edit link found");
  }

  final ODataEntityUpdateRequest<E> req;

  if (client.getConfiguration().isUseXHTTPMethod()) {
    req = new ODataEntityUpdateRequestImpl<>(client, HttpMethod.POST, entity.getEditLink(), entity);
    req.setXHTTPMethod(type.getMethod().name());
  } else {
    req = new ODataEntityUpdateRequestImpl<>(client, type.getMethod(), entity.getEditLink(), entity);
  }

  return req;
}
 
Example 9
Source File: CUDRequestFactoryImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public <E extends ClientEntity> ODataEntityUpdateRequest<E> getEntityUpdateRequest(
    final URI targetURI, final UpdateType type, final E changes) {

  final ODataEntityUpdateRequest<E> req;

  if (client.getConfiguration().isUseXHTTPMethod()) {
    req = new ODataEntityUpdateRequestImpl<>(client, HttpMethod.POST, targetURI, changes);
    req.setXHTTPMethod(type.getMethod().name());
  } else {
    req = new ODataEntityUpdateRequestImpl<>(client, type.getMethod(), targetURI, changes);
  }

  return req;
}
 
Example 10
Source File: CUDRequestFactoryImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataValueUpdateRequest getValueUpdateRequest(
    final URI targetURI, final UpdateType type, final ClientPrimitiveValue value) {

  final ODataValueUpdateRequest req;

  if (client.getConfiguration().isUseXHTTPMethod()) {
    req = new ODataValueUpdateRequestImpl(client, HttpMethod.POST, URIUtils.addValueSegment(targetURI), value);
    req.setXHTTPMethod(type.getMethod().name());
  } else {
    req = new ODataValueUpdateRequestImpl(client, type.getMethod(), URIUtils.addValueSegment(targetURI), value);
  }

  return req;
}
 
Example 11
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void noContentId() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.GET + " ESMedia" + HTTP_VERSION + CRLF
      + ACCEPT_HEADER
      + CRLF
      + CRLF
      + "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESMedia" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": image/png" + CRLF
      + "Content-Transfer-Encoding: base64" + CRLF
      + CRLF
      + "iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAABmJLR0QA/wD/AP+gvaeTAAAAH0lE"
      + "QVQokWNgGHmA8S4FmpkosXngNDP+PzdANg+cZgBqiQK5mkdWWgAAAABJRU5ErkJggg==" + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.PUT + " $1/PropertyInt16" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{\"value\":5}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  parse(batch);
}
 
Example 12
Source File: CUDRequestFactoryImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataReferenceAddingRequest getReferenceAddingRequest(final URI serviceRoot, final URI targetURI,
    final URI reference) {
  final URI contextURI = client.newURIBuilder(serviceRoot.toASCIIString()).appendMetadataSegment()
                                                                          .appendRefSegment().build();
  ResWrap<URI> wrappedPayload = new ResWrap<>(contextURI, null, reference);

  return new ODataReferenceAddingRequestImpl(client, HttpMethod.POST, targetURI, wrappedPayload);
}
 
Example 13
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void contentId() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.GET + " ESAllPrim" + HTTP_VERSION + CRLF
      + ACCEPT_HEADER
      + HttpHeader.CONTENT_ID + ": BBB" + CRLF
      + CRLF + CRLF
      + "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESMedia" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": image/png" + CRLF
      + "Content-Transfer-Encoding: base64" + CRLF
      + CRLF
      + "iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAABmJLR0QA/wD/AP+gvaeTAAAAH0lE"
      + "QVQokWNgGHmA8S4FmpkosXngNDP+PzdANg+cZgBqiQK5mkdWWgAAAABJRU5ErkJggg==" + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.PUT + " $1/PropertyInt16" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + HttpHeader.CONTENT_ID + ": 2" + CRLF
      + CRLF
      + "{\"value\":5}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  final List<BatchRequestPart> batchRequestParts = parse(batch);
  Assert.assertNotNull(batchRequestParts);

  for (BatchRequestPart multipart : batchRequestParts) {
    if (!multipart.isChangeSet()) {
      Assert.assertEquals(1, multipart.getRequests().size());
      Assert.assertEquals("BBB", multipart.getRequests().get(0).getHeader(HttpHeader.CONTENT_ID));
    } else {
      for (ODataRequest request : multipart.getRequests()) {
        if (HttpMethod.POST.equals(request.getMethod())) {
          Assert.assertEquals("1", request.getHeader(HttpHeader.CONTENT_ID));
        } else if (HttpMethod.PUT.equals(request.getMethod())) {
          Assert.assertEquals("2", request.getHeader(HttpHeader.CONTENT_ID));
          Assert.assertEquals("/$1/PropertyInt16", request.getRawODataPath());
          Assert.assertEquals(SERVICE_ROOT + "/$1/PropertyInt16", request.getRawRequestUri());
        }
      }
    }
  }
}
 
Example 14
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void preamble() throws Exception {
  final String batch = "This is a preamble and must be ignored" + CRLF
      + CRLF
      + CRLF
      + "----1242" + CRLF
      + "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.GET + " ESAllPrim" + HTTP_VERSION + CRLF
      + ACCEPT_HEADER
      + CRLF
      + CRLF
      + "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "This is a preamble and must be ignored" + CRLF
      + CRLF
      + CRLF
      + "----1242" + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESMedia" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": image/png" + CRLF
      + "Content-Transfer-Encoding: base64" + CRLF
      + CRLF
      + "iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAABmJLR0QA/wD/AP+gvaeTAAAAH0lE"
      + "QVQokWNgGHmA8S4FmpkosXngNDP+PzdANg+cZgBqiQK5mkdWWgAAAABJRU5ErkJggg==" + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 2" + CRLF
      + CRLF
      + HttpMethod.PUT + " $1/PropertyInt16" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{\"value\":5}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";
  final List<BatchRequestPart> batchRequestParts = parse(batch);

  Assert.assertNotNull(batchRequestParts);
  Assert.assertEquals(2, batchRequestParts.size());

  final BatchRequestPart getRequestPart = batchRequestParts.get(0);
  Assert.assertEquals(1, getRequestPart.getRequests().size());

  final ODataRequest getRequest = getRequestPart.getRequests().get(0);
  Assert.assertEquals(HttpMethod.GET, getRequest.getMethod());

  final BatchRequestPart changeSetPart = batchRequestParts.get(1);
  Assert.assertEquals(2, changeSetPart.getRequests().size());
  Assert.assertEquals("iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAABmJLR0QA/wD/AP+gvaeTAAAAH0lE"
      + "QVQokWNgGHmA8S4FmpkosXngNDP+PzdANg+cZgBqiQK5mkdWWgAAAABJRU5ErkJggg==" + CRLF,
      IOUtils.toString(changeSetPart.getRequests().get(0).getBody()));
  Assert.assertEquals("{\"value\":5}", IOUtils.toString(changeSetPart.getRequests().get(1).getBody()));
}
 
Example 15
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void methodsForIndividualRequests() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.POST + " ESAllPrim" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{ \"PropertyString\": \"Foo\" }"
      + CRLF
      + "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.DELETE + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + CRLF
      + CRLF
      + "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{ \"PropertyString\": \"Foo\" }" + CRLF
      + "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.PUT + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{ \"PropertyString\": \"Foo\" }" + CRLF
      + "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.GET + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + ACCEPT_HEADER
      + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  final List<BatchRequestPart> requests = parse(batch);

  Assert.assertEquals(HttpMethod.POST, requests.get(0).getRequests().get(0).getMethod());
  Assert.assertEquals("/ESAllPrim", requests.get(0).getRequests().get(0).getRawODataPath());
  Assert.assertEquals("{ \"PropertyString\": \"Foo\" }",
      IOUtils.toString(requests.get(0).getRequests().get(0).getBody()));

  Assert.assertEquals(HttpMethod.DELETE, requests.get(1).getRequests().get(0).getMethod());
  Assert.assertEquals("/ESAllPrim(32767)", requests.get(1).getRequests().get(0).getRawODataPath());

  Assert.assertEquals(HttpMethod.PATCH, requests.get(2).getRequests().get(0).getMethod());
  Assert.assertEquals("/ESAllPrim(32767)", requests.get(2).getRequests().get(0).getRawODataPath());
  Assert.assertEquals("{ \"PropertyString\": \"Foo\" }",
      IOUtils.toString(requests.get(2).getRequests().get(0).getBody()));

  Assert.assertEquals(HttpMethod.PUT, requests.get(3).getRequests().get(0).getMethod());
  Assert.assertEquals("/ESAllPrim(32767)", requests.get(3).getRequests().get(0).getRawODataPath());
  Assert.assertEquals("{ \"PropertyString\": \"Foo\" }",
      IOUtils.toString(requests.get(3).getRequests().get(0).getBody()));

  Assert.assertEquals(HttpMethod.GET, requests.get(4).getRequests().get(0).getMethod());
  Assert.assertEquals("/ESAllPrim(32767)", requests.get(4).getRequests().get(0).getRawODataPath());
}
 
Example 16
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public HttpMethod[] allowedMethods() {
  return new HttpMethod[] { HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT,
      HttpMethod.DELETE };
}
 
Example 17
Source File: BatchRequest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public HttpMethod[] allowedMethods() {
  return new HttpMethod[] {HttpMethod.POST};
}
 
Example 18
Source File: BatchHandler.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void validateHttpMethod(final ODataRequest request) throws BatchDeserializerException {
  if (request.getMethod() != HttpMethod.POST) {
    throw new BatchDeserializerException("Invalid HTTP method", MessageKeys.INVALID_METHOD, "0");
  }
}
 
Example 19
Source File: ODataMediaEntityCreateRequestImpl.java    From olingo-odata4 with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor.
 *
 * @param odataClient client instance getting this request
 * @param targetURI target entity set.
 * @param media media entity blob to be created.
 */
public ODataMediaEntityCreateRequestImpl(final ODataClient odataClient, final URI targetURI,
        final InputStream media) {

  super(odataClient, HttpMethod.POST, targetURI);
  this.media = media;
}
 
Example 20
Source File: ODataEntityCreateRequestImpl.java    From olingo-odata4 with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor.
 *
 * @param odataClient client instance getting this request
 * @param targetURI entity set URI.
 * @param entity entity to be created.
 */
ODataEntityCreateRequestImpl(final ODataClient odataClient, final URI targetURI, final E entity) {
  super(odataClient, HttpMethod.POST, targetURI);
  this.entity = entity;
}