Java Code Examples for org.apache.olingo.server.api.ODataRequest#setMethod()

The following examples show how to use org.apache.olingo.server.api.ODataRequest#setMethod() . 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: AsyncProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
static ODataRequest copyRequest(ODataRequest request) {
  ODataRequest req = new ODataRequest();
  req.setBody(copyRequestBody(request));
  req.setMethod(request.getMethod());
  req.setRawBaseUri(request.getRawBaseUri());
  req.setRawODataPath(request.getRawODataPath());
  req.setRawQueryPath(request.getRawQueryPath());
  req.setRawRequestUri(request.getRawRequestUri());
  req.setRawServiceResolutionUri(request.getRawServiceResolutionUri());

  for (Map.Entry<String, List<String>> header : request.getAllHeaders().entrySet()) {
    if (!HttpHeader.PREFER.toLowerCase().equals(
        header.getKey().toLowerCase())) {
      req.addHeader(header.getKey(), header.getValue());
    }
  }

  return req;
}
 
Example 2
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 3
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void handlerExtTest() 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");
  ODataHandlerImpl handler =  new ODataHandlerImpl(odata, serviceMetadata, new ServerCoreDebugger(odata));
  Processor extension =  new TechnicalActionProcessor(null, serviceMetadata);
  handler.register(extension);
  assertNull(handler.getLastThrownException());
  assertNull(handler.getUriInfo());
}
 
Example 4
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void dispatchEmptyContentWithoutContentType() {
  final String path = "ESAllPrim";
  final EntityCollectionProcessor processor = mock(EntityCollectionProcessor.class);
  
  ODataRequest request = new ODataRequest();
  request.setMethod(HttpMethod.POST);
  request.setRawBaseUri(BASE_URI);
  request.setRawRequestUri(BASE_URI);
  request.setRawODataPath(path);
  request.setBody(new ByteArrayInputStream(new byte[0]));

  final OData odata = OData.newInstance();
  final ServiceMetadata metadata = odata.createServiceMetadata(
      new EdmTechProvider(), Collections.<EdmxReference> emptyList());

  ODataHandlerImpl handler = new ODataHandlerImpl(odata, metadata, new ServerCoreDebugger(odata));

  if (processor != null) {
    handler.register(processor);
  }

  final ODataResponse response = handler.process(request);
  assertNotNull(response);
}
 
Example 5
Source File: ODataNettyHandlerImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * Extract the information part of Netty Request and fill OData Request
 * @param odRequest
 * @param httpRequest
 * @param split
 * @param contextPath
 * @return
 * @throws ODataLibraryException
 */
private ODataRequest fillODataRequest(final ODataRequest odRequest, final HttpRequest httpRequest,
     final int split, final String contextPath) throws ODataLibraryException {
   final int requestHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest");
   try {
   	ByteBuf byteBuf = ((HttpContent)httpRequest).content();
   	ByteBufInputStream inputStream = new ByteBufInputStream(byteBuf);
     odRequest.setBody(inputStream);
     
     odRequest.setProtocol(httpRequest.protocolVersion().text());
     odRequest.setMethod(extractMethod(httpRequest));
     int innerHandle = debugger.startRuntimeMeasurement("ODataNettyHandlerImpl", "copyHeaders");
     copyHeaders(odRequest, httpRequest);
     debugger.stopRuntimeMeasurement(innerHandle);
     innerHandle = debugger.startRuntimeMeasurement("ODataNettyHandlerImpl", "fillUriInformation");
     fillUriInformationFromHttpRequest(odRequest, httpRequest, split, contextPath);
     debugger.stopRuntimeMeasurement(innerHandle);

     return odRequest;
   } finally {
     debugger.stopRuntimeMeasurement(requestHandle);
   }
 }
 
Example 6
Source File: ODataHttpHandlerImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private ODataRequest fillODataRequest(final ODataRequest odRequest, final HttpServletRequest httpRequest,
    final int split) throws ODataLibraryException {
  final int requestHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest");
  try {
    odRequest.setBody(httpRequest.getInputStream());
    odRequest.setProtocol(httpRequest.getProtocol());
    odRequest.setMethod(extractMethod(httpRequest));
    int innerHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "copyHeaders");
    copyHeaders(odRequest, httpRequest);
    debugger.stopRuntimeMeasurement(innerHandle);
    innerHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillUriInformation");
    fillUriInformation(odRequest, httpRequest, split);
    debugger.stopRuntimeMeasurement(innerHandle);

    return odRequest;
  } catch (final IOException e) {
    throw new DeserializerException("An I/O exception occurred.", e,
        DeserializerException.MessageKeys.IO_EXCEPTION);
  } finally {
    debugger.stopRuntimeMeasurement(requestHandle);
  }
}
 
Example 7
Source File: DebugTabRequestTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void onlyProtocolNotSet() throws Exception {
  String expectedJson = "{\"method\":\"GET\",\"uri\":\"def&\",\"protocol\":\"unkown\"}";
  String expectedHtml = "<h2>Request Method</h2>\n"
      + "<p>GET</p>\n"
      + "<h2>Request URI</h2>\n"
      + "<p>def&amp;</p>\n"
      + "<h2>Request Protocol</h2>\n"
      + "<p>unkown</p>\n"
      + "<h2>Request Headers</h2>\n"
      + "<table>\n"
      + "<thead>\n"
      + "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
      + "</thead>\n"
      + "<tbody>\n"
      + "</tbody>\n"
      + "</table>\n";

  ODataRequest oDataRequest = new ODataRequest();
  oDataRequest.setMethod(HttpMethod.GET);
  oDataRequest.setRawRequestUri("def&");

  DebugTabRequest requestTab = new DebugTabRequest(oDataRequest);
  assertEquals(expectedJson, createJson(requestTab));
  assertEquals(expectedHtml, createHtml(requestTab));
}
 
Example 8
Source File: MockedBatchHandlerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = BatchDeserializerException.class)
public void testInvalidMethod() throws Exception {
  final String content = ""
      + "--batch_12345" + CRLF
      + "Content-Type: multipart/mixed; boundary=changeset_12345" + CRLF
      + CRLF
      + "--changeset_12345" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + "Content-Id: 1" + CRLF
      + CRLF
      + "PUT ESAllPrim(1) HTTP/1.1" + CRLF
      + "Content-Type: application/json;odata=verbose" + CRLF
      + CRLF
      + CRLF
      + "--changeset_12345--" + CRLF
      + CRLF
      + "--batch_12345--";

  final Map<String, List<String>> header = getMimeHeader();
  final ODataResponse response = new ODataResponse();
  final ODataRequest request = buildODataRequest(content, header);
  request.setMethod(HttpMethod.GET);

  batchHandler.process(request, response, true);
}
 
Example 9
Source File: MockedBatchHandlerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private ODataRequest buildODataRequest(final String content, final Map<String, List<String>> header)
    throws Exception {
  final ODataRequest request = new ODataRequest();

  for (final String key : header.keySet()) {
    request.addHeader(key, header.get(key));
  }

  request.setMethod(HttpMethod.POST);
  request.setRawBaseUri(BASE_URI);
  request.setRawODataPath(BATCH_ODATA_PATH);
  request.setRawQueryPath("");
  request.setRawRequestUri(BATCH_REQUEST_URI);
  request.setRawServiceResolutionUri("");

  request.setBody(new ByteArrayInputStream(content.getBytes("UTF-8")));

  return request;
}
 
Example 10
Source File: OData4HttpHandler.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
ODataRequest createODataRequest(final HttpServletRequest httpRequest, final int split)
    throws ODataLibraryException {
  try {
    ODataRequest odRequest = new ODataRequest();

    odRequest.setBody(httpRequest.getInputStream());
    copyHeaders(odRequest, httpRequest);
    odRequest.setMethod(extractMethod(httpRequest));
    fillUriInformation(odRequest, httpRequest, split);

    return odRequest;
  } catch (final IOException e) {
    throw new SerializerException(
        "An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION); //$NON-NLS-1$
  }
}
 
Example 11
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private ODataResponse dispatch(final HttpMethod method, final String path, final String query,
    final String headerName, final String headerValue, final Processor processor) {
  ODataRequest request = new ODataRequest();
  request.setMethod(method);
  request.setRawBaseUri(BASE_URI);
  if (path.isEmpty()) {
    request.setRawRequestUri(BASE_URI);
  }
  request.setRawODataPath(path);
  request.setRawQueryPath(query);

  if (headerName != null) {
    request.addHeader(headerName, Collections.singletonList(headerValue));
  }

  if (headerName != HttpHeader.CONTENT_TYPE) {
    request.addHeader(HttpHeader.CONTENT_TYPE, Collections.singletonList(
        ContentType.JSON.toContentTypeString()));
  }

  final OData odata = OData.newInstance();
  final ServiceMetadata metadata = odata.createServiceMetadata(
      new EdmTechProvider(), Collections.<EdmxReference> emptyList());

  ODataHandlerImpl handler = new ODataHandlerImpl(odata, metadata, new ServerCoreDebugger(odata));

  if (processor != null) {
    handler.register(processor);
  }

  final ODataResponse response = handler.process(request);
  assertNotNull(response);
  return response;
}
 
Example 12
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private ODataResponse dispatchToValidateHeaders(final HttpMethod method, final String path, final String query,
    final Map<String, String> headers, final Processor processor) throws ODataHandlerException {
  ODataRequest request = new ODataRequest();
  request.setMethod(method);
  request.setRawBaseUri(BASE_URI);
  for (Entry<String, String> header : headers.entrySet()) {
    request.addHeader(header.getKey(), header.getValue());
  }
  if (path.isEmpty()) {
    request.setRawRequestUri(BASE_URI);
  }
  request.setRawODataPath(path);
  request.setRawQueryPath(query);

  final OData odata = OData.newInstance();
  final ServiceMetadata metadata = odata.createServiceMetadata(
      new EdmTechProvider(), Collections.<EdmxReference> emptyList());

  ODataHandlerImpl handler = new ODataHandlerImpl(odata, metadata, new ServerCoreDebugger(odata));

  if (processor != null) {
    handler.register(processor);
  }

  final ODataResponse response = handler.process(request);
  return response;
}
 
Example 13
Source File: BatchRequestTransformator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private ODataRequest createRequest(final BatchQueryOperation operation, final String baseUri,
    final boolean isChangeSet) throws BatchDeserializerException {
  final HttpRequestStatusLine statusLine =
      new HttpRequestStatusLine(operation.getHttpStatusLine(), baseUri, rawServiceResolutionUri);
  statusLine.validateHttpMethod(isChangeSet);
  BatchTransformatorCommon.validateHost(operation.getHeaders(), baseUri);

  validateBody(statusLine, operation);
  Charset charset = getCharset(operation);
  InputStream bodyStream = getBodyStream(operation, statusLine, charset);

  validateForbiddenHeader(operation);

  final ODataRequest request = new ODataRequest();
  request.setBody(bodyStream);
  request.setMethod(statusLine.getMethod());
  request.setRawBaseUri(statusLine.getRawBaseUri());
  request.setRawODataPath(statusLine.getRawODataPath());
  request.setRawQueryPath(statusLine.getRawQueryPath());
  request.setRawRequestUri(statusLine.getRawRequestUri());
  request.setRawServiceResolutionUri(statusLine.getRawServiceResolutionUri());

  for (final HeaderField field : operation.getHeaders()) {
    request.addHeader(field.getFieldName(), field.getValues());
  }

  return request;
}
 
Example 14
Source File: DebugTabRequestTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void singleHeaderValue() throws Exception {
  String expectedJson =
      "{\"method\":\"GET\",\"uri\":\"def&\",\"protocol\":\"def&\",\"headers\":{\"HeaderName\":\"Value1\"}}";
  String expectedHtml = "<h2>Request Method</h2>\n"
      + "<p>GET</p>\n"
      + "<h2>Request URI</h2>\n"
      + "<p>def&amp;</p>\n"
      + "<h2>Request Protocol</h2>\n"
      + "<p>def&amp;</p>\n"
      + "<h2>Request Headers</h2>\n"
      + "<table>\n"
      + "<thead>\n"
      + "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
      + "</thead>\n"
      + "<tbody>\n"
      + "<tr><td class=\"name\">HeaderName</td><td class=\"value\">Value1</td></tr>\n"
      + "</tbody>\n"
      + "</table>\n";

  ODataRequest oDataRequest = new ODataRequest();
  oDataRequest.setMethod(HttpMethod.GET);
  oDataRequest.setRawRequestUri("def&");
  oDataRequest.setProtocol("def&");
  List<String> headerValues = new ArrayList<String>();
  headerValues.add("Value1");
  oDataRequest.addHeader("HeaderName", headerValues);

  DebugTabRequest requestTab = new DebugTabRequest(oDataRequest);
  assertEquals(expectedJson, createJson(requestTab));
  assertEquals(expectedHtml, createHtml(requestTab));
}
 
Example 15
Source File: DebugTabRequestTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void multiHeaderValueResultsInMap() throws Exception {
  String expectedJson = "{\"method\":\"GET\",\"uri\":\"def&\",\"protocol\":\"def&\","
      + "\"headers\":{\"HeaderName\":[\"Value1\",\"Value2\"]}}";
  String expectedHtml = "<h2>Request Method</h2>\n"
      + "<p>GET</p>\n"
      + "<h2>Request URI</h2>\n"
      + "<p>def&amp;</p>\n"
      + "<h2>Request Protocol</h2>\n"
      + "<p>def&amp;</p>\n"
      + "<h2>Request Headers</h2>\n"
      + "<table>\n"
      + "<thead>\n"
      + "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
      + "</thead>\n"
      + "<tbody>\n"
      + "<tr><td class=\"name\">HeaderName</td><td class=\"value\">Value1</td></tr>\n"
      + "<tr><td class=\"name\">HeaderName</td><td class=\"value\">Value2</td></tr>\n"
      + "</tbody>\n"
      + "</table>\n";

  ODataRequest oDataRequest = new ODataRequest();
  oDataRequest.setMethod(HttpMethod.GET);
  oDataRequest.setRawRequestUri("def&");
  oDataRequest.setProtocol("def&");
  List<String> headerValues = new ArrayList<String>();
  headerValues.add("Value1");
  headerValues.add("Value2");
  oDataRequest.addHeader("HeaderName", headerValues);

  DebugTabRequest requestTab = new DebugTabRequest(oDataRequest);
  assertEquals(expectedJson, createJson(requestTab));
  assertEquals(expectedHtml, createHtml(requestTab));
}