org.apache.olingo.commons.api.http.HttpMethod Java Examples

The following examples show how to use org.apache.olingo.commons.api.http.HttpMethod. 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: DerivedAndMixedTypeTestITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void queryESCompCollDerivedXml() throws Exception {
  URL url = new URL(SERVICE_URI + "ESCompCollDerived?$format=xml");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.APPLICATION_XML, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("<d:PropertyCompAno m:type=\"#olingo.odata.test1.CTBaseAno\">" +
      "<d:PropertyString>Num111</d:PropertyString>" +
      "<d:AdditionalPropString>Test123</d:AdditionalPropString>" +
      "</d:PropertyCompAno>" +
      "<d:CollPropertyCompAno m:type=\"#Collection(olingo.odata.test1.CTTwoPrimAno)\">" +
      "<m:element m:type=\"olingo.odata.test1.CTBaseAno\">" +
      "<d:PropertyString>TEST12345</d:PropertyString>" +
      "<d:AdditionalPropString>Additional12345</d:AdditionalPropString>" ));
}
 
Example #2
Source File: BasicStreamITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void streamESStreamJson() throws Exception {
  URL url = new URL(SERVICE_URI + "ESStream?$format=json");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());

  assertTrue(content.contains("[email protected]\"," +
          "\"[email protected]\"," +
          "\"[email protected]\""));
  assertTrue(content.contains("\"PropertyString\":\"TEST 1->streamed\""));
  assertTrue(content.contains("\"PropertyString\":\"TEST 2->streamed\""));
}
 
Example #3
Source File: SelectOnComplexPropertiesITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void queryESKeyColPropertyComp1() throws Exception {
  URL url = new URL(SERVICE_URI + "ESKeyNav(1)/CollPropertyComp"
      + "?$select=PropertyComp/PropertyString");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("\"value\":[{\"PropertyComp\":"
          + "{\"PropertyString\":\"First Resource - positive values\"}},"
          + "{\"PropertyComp\":{\"PropertyString\":\"First Resource - positive values\"}},"
          + "{\"PropertyComp\":{\"PropertyString\":\"First Resource - positive values\"}}]"));
}
 
Example #4
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void dispatchPrimitiveProperty() throws Exception {
  final String uri = "ESAllPrim(0)/PropertyString";
  final PrimitiveProcessor processor = mock(PrimitiveProcessor.class);

  dispatch(HttpMethod.GET, uri, processor);
  verify(processor).readPrimitive(
      any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class));

  dispatch(HttpMethod.PATCH, uri, processor);
  verify(processor).updatePrimitive(
      any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class),
      any(ContentType.class));

  dispatch(HttpMethod.PUT, uri, processor);
  verify(processor, times(2)).updatePrimitive(
      any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class),
      any(ContentType.class));

  dispatch(HttpMethod.DELETE, uri, processor);
  verify(processor).deletePrimitive(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class));

  dispatchMethodNotAllowed(HttpMethod.POST, uri, processor);
  dispatchMethodNotAllowed(HttpMethod.HEAD, uri, processor);
}
 
Example #5
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = this.odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #6
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 #7
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void uriParserExceptionResultsInRightResponseEdmCause() throws Exception {
  final OData odata = OData.newInstance();
  final ServiceMetadata serviceMetadata = odata.createServiceMetadata(
      new CsdlAbstractEdmProvider() {
        @Override
        public CsdlEntitySet getEntitySet(final FullQualifiedName entityContainer, final String entitySetName)
            throws ODataException {
          throw new ODataException("msg");
        }
      },
      Collections.<EdmxReference> emptyList());

  ODataRequest request = new ODataRequest();
  request.setMethod(HttpMethod.GET);
  request.setRawODataPath("EdmException");

  final ODataResponse response =
      new ODataHandlerImpl(odata, serviceMetadata, new ServerCoreDebugger(odata)).process(request);
  assertNotNull(response);
  assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode());
}
 
Example #8
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #9
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 #10
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void serviceDocumentDefault() throws Exception {
  final ODataResponse response = dispatch(HttpMethod.GET, "/", null);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  String ct = response.getHeader(HttpHeader.CONTENT_TYPE);
  assertThat(ct, containsString("application/json"));
  assertThat(ct, containsString("odata.metadata=minimal"));

  assertNotNull(response.getContent());
  String doc = IOUtils.toString(response.getContent());

  assertThat(doc, containsString("\"@odata.context\":\"$metadata\""));
  assertThat(doc, containsString("\"value\":"));
  
  final ODataResponse response2 = dispatch(HttpMethod.HEAD, "/", null);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response2.getStatusCode());
  assertNull(response2.getHeader(HttpHeader.CONTENT_TYPE));
  assertNull(response2.getContent());
}
 
Example #11
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 #12
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #13
Source File: DerivedAndMixedTypeTestITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void queryESAllPrimDerivedJson() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrimDerived(0)?$expand=NavPropertyETTwoPrimMany&$format=json");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("\"@odata.type\":\"#olingo.odata.test1.ETBase\"," +
      "\"PropertyInt16\":32766," +
      "\"PropertyString\":\"Test String1\"," +
      "\"AdditionalPropertyString_5\":\"Additional String1\"" ));
}
 
Example #14
Source File: AcceptHeaderAcceptCharsetHeaderITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void formatWithAcceptCharset() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim?$format=application/json;charset=utf-8");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "abc");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());

  assertNotNull(connection.getHeaderField(HttpHeader.CONTENT_TYPE));
  ContentType contentType = ContentType.parse(connection.getHeaderField(HttpHeader.CONTENT_TYPE));
  assertEquals("application", contentType.getType());
  assertEquals("json", contentType.getSubtype());
  assertEquals(2, contentType.getParameters().size());
  assertEquals("minimal", contentType.getParameter("odata.metadata"));
  assertEquals("utf-8", contentType.getParameter("charset"));

  final String content = IOUtils.toString(connection.getInputStream());
  assertNotNull(content);
}
 
Example #15
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * This method is invoked for PATCH or PUT requests
 */
public void updateEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams, Entity updateEntity,
    HttpMethod httpMethod) throws ODataApplicationException {

  EdmEntityType edmEntityType = edmEntitySet.getEntityType();
  
  if (edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)) {
    updateEntity(edmEntityType, keyParams, updateEntity, httpMethod, 
        manager.getEntityCollection(DemoEdmProvider.ES_PRODUCTS_NAME));
  } else if (edmEntitySet.getName().equals(DemoEdmProvider.ES_CATEGORIES_NAME)) {
    updateEntity(edmEntityType, keyParams, updateEntity, httpMethod, 
        manager.getEntityCollection(DemoEdmProvider.ES_CATEGORIES_NAME));
  } else if(edmEntitySet.getName().equals(DemoEdmProvider.ES_ADVERTISEMENTS_NAME)) {
    updateEntity(edmEntityType, keyParams, updateEntity, httpMethod, 
        manager.getEntityCollection(DemoEdmProvider.ES_ADVERTISEMENTS_NAME));
  }
}
 
Example #16
Source File: BasicHttpITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubstringOfWithParameterAlias() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim?$filter=substringof(@word,PropertyString)&@word='Second'");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  String content = IOUtils.toString(connection.getInputStream());
  assertNotNull(content);
  assertTrue(content.contains("\"value\":[{\"PropertyInt16\":-32768,"
      + "\"PropertyString\":\"Second Resource - negative values\","
      + "\"PropertyBoolean\":false,\"PropertyByte\":0,\"PropertySByte\":-128,\"PropertyInt32\":-2147483648,"
      + "\"PropertyInt64\":-9223372036854775808,\"PropertySingle\":-1.79E8,\"PropertyDouble\":-179000.0,"
      + "\"PropertyDecimal\":-34,\"PropertyBinary\":\"ASNFZ4mrze8=\",\"PropertyDate\":\"2015-11-05\","
      + "\"PropertyDateTimeOffset\":\"2005-12-03T07:17:08Z\",\"PropertyDuration\":\"PT9S\","
      + "\"PropertyGuid\":\"76543201-23ab-cdef-0123-456789dddfff\",\"PropertyTimeOfDay\":\"23:49:14\"}]}"));
  connection.disconnect();
}
 
Example #17
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 #18
Source File: BasicHttpITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithNoFormEncoding() throws Exception {
  URL url = new URL(SERVICE_URI + 
      "ESAllPrim?$filter=PropertyInt16+eq+1");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=full");
  connection.connect();

  assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode());
}
 
Example #19
Source File: InOperatorITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void queryInOperatorOnNavProperty() throws Exception {
  URL url = new URL(SERVICE_URI + "ESKeyNav?$filter=PropertyCompTwoPrim/"
      + "PropertyInt16%20in%20NavPropertyETKeyNavOne/CollPropertyInt16");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("\"value\":[]"));
}
 
Example #20
Source File: BasicHttpITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithFormEncoding() throws Exception {
  URL url = new URL(SERVICE_URI + 
      "ESAllPrim?$filter=PropertyInt16+eq+1&odata-accept-forms-encoding=true");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=full");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  final String content = IOUtils.toString(connection.getInputStream());

  assertNotNull(content);
}
 
Example #21
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 #22
Source File: BasicStreamITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void expandStreamPropOnComplexTypeJson() throws Exception {
  URL url = new URL(SERVICE_URI + "ESStreamOnComplexProp(7)?$expand=PropertyCompWithStream/PropertyStream,"
      + "PropertyEntityStream,PropertyCompWithStream/NavPropertyETStreamOnComplexPropOne($expand=PropertyStream),"
      + "PropertyCompWithStream/NavPropertyETStreamOnComplexPropMany/$count&$format=json");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty("OData-Version", "4.01");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());

  assertTrue(content.contains("\"NavPropertyETStreamOnComplexPropOne\":{"
      + "\"PropertyInt16\":7,"
      + "\"PropertyStream@mediaEtag\":\"eTag\","
      + "\"PropertyStream@mediaContentType\":\"image/jpeg\","
      + "\"PropertyStream\":\"\ufffdioz\ufffd\\\"\ufffd\"}"));
  assertTrue(content.contains("\"NavPropertyETStreamOnComplexPropMany@count\":2"));
  assertTrue(content.contains("\"PropertyCompWithStream\":{"
      + "\"PropertyStream@mediaEtag\":\"eTag\","
      + "\"PropertyStream@mediaContentType\":\"image/jpeg\","
      + "\"PropertyStream\":\"\ufffdioz\ufffd\\\"\ufffd\","
      + "\"PropertyComp\":{\"PropertyInt16\":333,\"PropertyString\":\"TEST123\"}"));
  assertFalse(content.contains("\"PropertyInt16\":7,"
      + "\"PropertyInt32\":10,"
      + "\"PropertyEntityStream@mediaEtag\":\"eTag\","
      + "\"PropertyEntityStream@mediaContentType\":\"image/jpeg\","
      + "\"PropertyEntityStream\":\"ufffdioz\ufffd\\\"\ufffd\""));
  }
 
Example #23
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * This method is invoked for PATCH or PUT requests
 * */
public void updateEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams, Entity updateEntity,
    HttpMethod httpMethod) throws ODataApplicationException {

  EdmEntityType edmEntityType = edmEntitySet.getEntityType();

  // actually, this is only required if we have more than one Entity Type
  if (edmEntityType.getName().equals(DemoEdmProvider.ET_PRODUCT_NAME)) {
    updateProduct(edmEntityType, keyParams, updateEntity, httpMethod);
  }
}
 
Example #24
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void serviceDocumentRedirect() throws Exception {
  final ODataResponse response = dispatch(HttpMethod.GET, "", null);
  assertEquals(HttpStatusCode.TEMPORARY_REDIRECT.getStatusCode(), response.getStatusCode());
  assertEquals(BASE_URI + "/", response.getHeader(HttpHeader.LOCATION));
  
  final ODataResponse responseHead = dispatch(HttpMethod.HEAD, "", null);
  assertEquals(HttpStatusCode.TEMPORARY_REDIRECT.getStatusCode(), responseHead.getStatusCode());
  assertEquals(BASE_URI + "/", responseHead.getHeader(HttpHeader.LOCATION));
}
 
Example #25
Source File: CUDRequestFactoryImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public <E extends ClientEntity> ODataMediaEntityUpdateRequest<E> getMediaEntityUpdateRequest(
    final URI editURI, final InputStream media) {

  final ODataMediaEntityUpdateRequest<E> req;

  if (client.getConfiguration().isUseXHTTPMethod()) {
    req = new ODataMediaEntityUpdateRequestImpl<>(client, HttpMethod.POST, URIUtils.addValueSegment(editURI), media);
    req.setXHTTPMethod(HttpMethod.PUT.name());
  } else {
    req = new ODataMediaEntityUpdateRequestImpl<>(client, HttpMethod.PUT, URIUtils.addValueSegment(editURI), media);
  }

  return req;
}
 
Example #26
Source File: DerivedAndMixedTypeTestITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void queryESAllPrimWithEntityTypeCastInExpand() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim(0)?$expand=NavPropertyETTwoPrimOne/olingo.odata.test1.ETBase");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, 
      ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("\"PropertyInt16\":0,"
      + "\"PropertyString\":\"\","
      + "\"PropertyBoolean\":false,"
      + "\"PropertyByte\":0,"
      + "\"PropertySByte\":0,"
      + "\"PropertyInt32\":0,"
      + "\"PropertyInt64\":0,"
      + "\"PropertySingle\":0.0,"
      + "\"PropertyDouble\":0.0,"
      + "\"PropertyDecimal\":0,"
      + "\"PropertyBinary\":\"\","
      + "\"PropertyDate\":\"1970-01-01\","
      + "\"PropertyDateTimeOffset\":\"2005-12-03T00:00:00Z\","
      + "\"PropertyDuration\":\"PT0S\","
      + "\"PropertyGuid\":\"76543201-23ab-cdef-0123-456789cccddd\","
      + "\"PropertyTimeOfDay\":\"00:01:01\",\"NavPropertyETTwoPrimOne\":{"
      + "\"@odata.type\":\"#olingo.odata.test1.ETBase\","
      + "\"PropertyInt16\":111,\"PropertyString\":\"TEST A\","
      + "\"AdditionalPropertyString_5\":\"TEST A 0815\"}"));
}
 
Example #27
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 #28
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void uriParserExceptionWithFormatJsonAcceptAtom() throws Exception {
  final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json",
      HttpHeader.ACCEPT, ContentType.APPLICATION_ATOM_XML.toContentTypeString(), null);
  assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
  assertEquals("application/json;odata.metadata=minimal",
      response.getHeader(HttpHeader.CONTENT_TYPE));
}
 
Example #29
Source File: SelectOnComplexPropertiesITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void queryESKeyColPropertyComp2() throws Exception {
  URL url = new URL(SERVICE_URI + "ESKeyNav(1)/CollPropertyComp"
      + "?$select=PropertyInt16");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("\"value\":[{\"PropertyInt16\":1},{\"PropertyInt16\":2},{\"PropertyInt16\":3}]"));
}
 
Example #30
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void postWithoutBody() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": changeRequest1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESAllPrim" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 100" + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_OCTET_STREAM + CRLF
      + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";
  final List<BatchRequestPart> batchRequestParts = parse(batch);

  Assert.assertEquals(1, batchRequestParts.size());
  Assert.assertTrue(batchRequestParts.get(0).isChangeSet());
  Assert.assertEquals(1, batchRequestParts.get(0).getRequests().size());
  final ODataRequest request = batchRequestParts.get(0).getRequests().get(0);
  Assert.assertEquals(HttpMethod.POST, request.getMethod());
  Assert.assertEquals("100", request.getHeader(HttpHeader.CONTENT_LENGTH));
  Assert.assertEquals(APPLICATION_OCTET_STREAM, request.getHeader(HttpHeader.CONTENT_TYPE));
  Assert.assertNotNull(request.getBody());
  Assert.assertEquals(-1, request.getBody().read());
}