org.apache.olingo.odata2.api.ODataService Java Examples

The following examples show how to use org.apache.olingo.odata2.api.ODataService. 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: ServiceFactory.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public ODataService createService(ODataContext ctx) throws ODataException
{
   ODataService res = null;

   // Gets the last `root` segment of the URL
   // Stores this value in the `serviceName` variable
   // if the URL is http://dhus.gael.fr/odata/v1/Products/...
   //               \__________________________/\_________...
   //                          ROOT                ODATA
   // serviceName:="v1"
   // The length of the `root` part of the URL can be extended with the servlet's split parameter.
   // see http://http://olingo.apache.org/doc/odata2/index.html
   List<PathSegment> pathSegs = ctx.getPathInfo().getPrecedingSegments();
   String serviceName = pathSegs.get(pathSegs.size() - 1).getPath();

   if (serviceName.equals(SERVICE_NAME))
   {
      EdmProvider edmProvider = new Model();
      ODataSingleProcessor oDataProcessor = new Processor();

      res = createODataSingleProcessorService(edmProvider, oDataProcessor);
   }

   return res;
}
 
Example #2
Source File: BatchHandlerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Before
public void setupBatchHandler() throws Exception {
  ODataProcessor processor = new LocalProcessor();
  ODataService serviceMock = mock(ODataService.class);
  when(serviceMock.getBatchProcessor()).thenReturn((BatchProcessor) processor);
  when(serviceMock.getEntitySetProcessor()).thenReturn((EntitySetProcessor) processor);
  when(serviceMock.getEntitySimplePropertyProcessor()).thenReturn((EntitySimplePropertyProcessor) processor);
  when(serviceMock.getProcessor()).thenReturn(processor);
  Edm mockEdm = MockFacade.getMockEdm();
  when(serviceMock.getEntityDataModel()).thenReturn(mockEdm);
  List<String> supportedContentTypes = Arrays.asList(
      HttpContentType.APPLICATION_JSON_UTF8, HttpContentType.APPLICATION_JSON);
  when(serviceMock.getSupportedContentTypes(EntityMediaProcessor.class)).thenReturn(supportedContentTypes);
  when(serviceMock.getSupportedContentTypes(EntityProcessor.class)).thenReturn(supportedContentTypes);
  when(serviceMock.getSupportedContentTypes(EntitySimplePropertyProcessor.class)).thenReturn(supportedContentTypes);
  handler = new BatchHandlerImpl(mock(ODataServiceFactory.class), serviceMock);
}
 
Example #3
Source File: JPAServiceFactory.java    From lemonaid with MIT License 6 votes vote down vote up
/**
 * Creates an OData Service based on the values set in
 * {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext} and
 * {@link org.apache.olingo.odata2.api.processor.ODataContext}.
 */
@Override
public final ODataService createService(final ODataContext ctx) throws ODataException {

	oDataContext = ctx;

	// Initialize OData JPA Context
	oDataJPAContext = initializeODataJPAContext();

	validatePreConditions();

	ODataJPAFactory factory = ODataJPAFactory.createFactory();
	ODataJPAAccessFactory accessFactory = factory.getODataJPAAccessFactory();

	// OData JPA Processor
	if (oDataJPAContext.getODataContext() == null) {
		oDataJPAContext.setODataContext(ctx);
	}

	ODataSingleProcessor odataJPAProcessor = new ODataJPAProcessor(oDataJPAContext);

	// OData Entity Data Model Provider based on JPA
	EdmProvider edmProvider = accessFactory.createJPAEdmProvider(oDataJPAContext);

	return createODataSingleProcessorService(edmProvider, odataJPAProcessor);
}
 
Example #4
Source File: ODataRequestHandlerValidationTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private void executeAndValidateRequest(final ODataHttpMethod method,
    final List<String> pathSegments,
    final Map<String, String> queryParameters,
    final String httpHeaderName, final String httpHeaderValue,
    final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws ODataException {

  ODataServiceFactory serviceFactory = mock(ODataServiceFactory.class);
  final ODataService service = mockODataService(serviceFactory);
  when(serviceFactory.createService(any(ODataContext.class))).thenReturn(service);

  final ODataRequest request = mockODataRequest(method, pathSegments, queryParameters,
      httpHeaderName, httpHeaderValue, requestContentType);
  final ODataContextImpl context = new ODataContextImpl(request, serviceFactory);

  final ODataResponse response = new ODataRequestHandler(serviceFactory, service, context).handle(request);
  assertNotNull(response);
  assertEquals(expectedStatusCode == null ? HttpStatusCodes.PAYMENT_REQUIRED : expectedStatusCode,
      response.getStatus());
}
 
Example #5
Source File: ODataServletTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serviceInstance() throws Exception {
  ODataServlet servlet = new ODataServlet();
  prepareServlet(servlet);
  prepareRequest(reqMock, "", "/servlet-path");
  Mockito.when(reqMock.getPathInfo()).thenReturn("/request-path-info");
  Mockito.when(reqMock.getRequestURI()).thenReturn("http://localhost:8080/servlet-path/request-path-info");
  ODataServiceFactory factory = Mockito.mock(ODataServiceFactory.class);
  ODataService service = Mockito.mock(ODataService.class);
  Mockito.when(factory.createService(Mockito.any(ODataContext.class))).thenReturn(service);
  ODataProcessor processor = Mockito.mock(ODataProcessor.class);
  Mockito.when(service.getProcessor()).thenReturn(processor);
  Mockito.when(reqMock.getAttribute(ODataServiceFactory.FACTORY_INSTANCE_LABEL)).thenReturn(factory);
  Mockito.when(respMock.getOutputStream()).thenReturn(Mockito.mock(ServletOutputStream.class));

  servlet.service(reqMock, respMock);

  Mockito.verify(factory).createService(Mockito.any(ODataContext.class));
}
 
Example #6
Source File: FitStaticServiceFactory.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
  public ODataService createService(final ODataContext ctx) throws ODataException {

    assertNotNull(ctx);
    assertNotNull(ctx.getAcceptableLanguages());
    assertNotNull(ctx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT));

    final Map<String, List<String>> requestHeaders = ctx.getRequestHeaders();
    final String host = requestHeaders.get("Host").get(0);

    String tmp[] = host.split(":", 2);
    String port = (tmp.length == 2 && tmp[1] != null) ? tmp[1] : "80";

    // access and validation in synchronized block
    synchronized (PORT_2_SERVICE) {
      final ODataService service = PORT_2_SERVICE.get(port);
//      if (service == null) {
//        throw new IllegalArgumentException("no static service set for JUnit test");
//      }
      return service;
    }
  }
 
Example #7
Source File: ODataSubLocator.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private Response handle(final ODataHttpMethod method) throws ODataException {
  request = ODataRequest.fromRequest(request).method(method).build();

  ODataContextImpl context = new ODataContextImpl(request, serviceFactory);
  context.setParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT, httpRequest);

  ODataService service = serviceFactory.createService(context);
  if(service == null){
    return returnNoServiceResponse(ODataInternalServerErrorException.NOSERVICE);
  }
  service.getProcessor().setContext(context);
  context.setService(service);

  ODataRequestHandler requestHandler = new ODataRequestHandler(serviceFactory, service, context);

  final ODataResponse odataResponse = requestHandler.handle(request);
  final Response response = RestUtil.convertResponse(odataResponse);

  return response;
}
 
Example #8
Source File: DispatcherTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void
    negotiateContentTypeCharset(final String requestType, final String supportedType, final boolean asFormat)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException,
        ODataException {

  ODataServiceFactory factory = mock(ODataServiceFactory.class);
  ODataService service = Mockito.mock(ODataService.class);
  Dispatcher dispatcher = new Dispatcher(factory, service);

  UriInfoImpl uriInfo = new UriInfoImpl();
  uriInfo.setUriType(UriType.URI1); //
  if (asFormat) {
    uriInfo.setFormat(requestType);
  }

  Mockito.when(service.getSupportedContentTypes(Matchers.any(Class.class))).thenReturn(Arrays.asList(supportedType));
  EntitySetProcessor processor = Mockito.mock(EntitySetProcessor.class);
  ODataResponse response = Mockito.mock(ODataResponse.class);
  Mockito.when(response.getContentHeader()).thenReturn(supportedType);
  Mockito.when(processor.readEntitySet(uriInfo, supportedType)).thenReturn(response);
  Mockito.when(service.getEntitySetProcessor()).thenReturn(processor);

  InputStream content = null;
  ODataResponse odataResponse =
      dispatcher.dispatch(ODataHttpMethod.GET, uriInfo, content, requestType, supportedType);
  assertEquals(supportedType, odataResponse.getContentHeader());
}
 
Example #9
Source File: ScenarioServiceFactory.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataService createService(final ODataContext context) throws ODataException {
  DataContainer dataContainer = new DataContainer();
  dataContainer.reset();

  return createODataSingleProcessorService(
      new ScenarioEdmProvider(),
      new ListsProcessor(new ScenarioDataSource(dataContainer)));
}
 
Example #10
Source File: TestServer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public void startServer(final ODataService service) {
  startServer(FitStaticServiceFactory.class);

  if ((server != null) && server.isStarted()) {
    FitStaticServiceFactory.bindService(this, service);
  }
}
 
Example #11
Source File: TestServer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public void startServer(final ODataService service, Class<? extends FitStaticServiceFactory> clazz) {
  startServer(clazz);

  if ((server != null) && server.isStarted()) {
    FitStaticServiceFactory.bindService(this, service);
  }
}
 
Example #12
Source File: MapFactory.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataService createService(final ODataContext ctx) throws ODataException {
  final MapProvider provider = new MapProvider();
  final MapProcessor processor = new MapProcessor();

  return createODataSingleProcessorService(provider, processor);
}
 
Example #13
Source File: ODataJPAServiceFactory.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an OData Service based on the values set in
 * {@link org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext} and
 * {@link org.apache.olingo.odata2.api.processor.ODataContext}.
 */
@Override
public final ODataService createService(final ODataContext ctx) throws ODataException {

  oDataContext = ctx;

  // Initialize OData JPA Context
  oDataJPAContext = initializeODataJPAContext();

  validatePreConditions();

  ODataJPAFactory factory = ODataJPAFactory.createFactory();
  ODataJPAAccessFactory accessFactory = factory.getODataJPAAccessFactory();

  // OData JPA Processor
  if (oDataJPAContext.getODataContext() == null) {
    oDataJPAContext.setODataContext(ctx);
  }

  ODataSingleProcessor odataJPAProcessor = createCustomODataProcessor(oDataJPAContext);
  if(odataJPAProcessor == null) {
    odataJPAProcessor = accessFactory.createODataProcessor(oDataJPAContext);
  }
  // OData Entity Data Model Provider based on JPA
  EdmProvider edmProvider = accessFactory.createJPAEdmProvider(oDataJPAContext);

  return createODataSingleProcessorService(edmProvider, odataJPAProcessor);
}
 
Example #14
Source File: ODataServiceMock.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public ODataService mock() throws ODataException {
  ODataService odataService = EasyMock.createMock(ODataService.class);
  EasyMock.expect(odataService.getEntityDataModel()).andReturn(mockEdm());
  EasyMock.replay(odataService);
  return odataService;

}
 
Example #15
Source File: AbstractContentNegotiationTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
protected ODataService createService() throws ODataException {
  DataContainer dataContainer = new DataContainer();
  dataContainer.init();
  final ODataSingleProcessor processor = new ListsProcessor(new ScenarioDataSource(dataContainer));
  final EdmProvider provider = new ScenarioEdmProvider();
  return new ODataSingleProcessorService(provider, processor) {};
}
 
Example #16
Source File: ClientDeltaResponseTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
protected ODataService createService() throws ODataException {
  EdmProvider provider = new ScenarioEdmProvider();
  processor = new StubProcessor();

  return new ODataSingleProcessorService(provider, processor);
}
 
Example #17
Source File: BasicBatchTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
protected ODataService createService() throws ODataException {
  final EdmProvider provider = createEdmProvider();

  final ODataSingleProcessor processor = createProcessor();

  return new ODataSingleProcessorService(provider, processor) {
    Edm edm = MockFacade.getMockEdm();

    @Override
    public Edm getEntityDataModel() throws ODataException {
      return edm;
    }
  };
}
 
Example #18
Source File: AnnotationServiceFactoryImplTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ODataException.class)
public void createFromClasses() throws ODataException {
  AnnotationServiceFactoryImpl factory = new AnnotationServiceFactoryImpl();

  final Collection<Class<?>> notAnnotatedClasses = new ArrayList<Class<?>>();
  notAnnotatedClasses.add(String.class);
  notAnnotatedClasses.add(Long.class);
  ODataService service = factory.createAnnotationService(notAnnotatedClasses);

  Assert.assertNotNull(service);
}
 
Example #19
Source File: AnnotationServiceFactoryImplTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void createFromAnnotatedClasses() throws ODataException {
  AnnotationServiceFactoryImpl factory = new AnnotationServiceFactoryImpl();
  final Collection<Class<?>> annotatedClasses = new ArrayList<Class<?>>();
  annotatedClasses.add(RefBase.class);
  annotatedClasses.add(Building.class);
  annotatedClasses.add(Employee.class);
  annotatedClasses.add(Manager.class);
  annotatedClasses.add(Photo.class);
  annotatedClasses.add(Room.class);
  annotatedClasses.add(Team.class);
  ODataService service = factory.createAnnotationService(annotatedClasses);

  Assert.assertNotNull(service);
}
 
Example #20
Source File: AnnotationServiceFactoryImplTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void createFromPackage() throws ODataException {
  AnnotationServiceFactoryImpl factory = new AnnotationServiceFactoryImpl();
  ODataService service = factory.createAnnotationService(Building.class.getPackage().getName());

  Assert.assertNotNull(service);
}
 
Example #21
Source File: AnnotationServiceFactoryImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ODataService createAnnotationService(final Collection<Class<?>> annotatedClasses) throws ODataException {
  AnnotationEdmProvider edmProvider = new AnnotationEdmProvider(annotatedClasses);
  AnnotationInMemoryDs dataSource = new AnnotationInMemoryDs(annotatedClasses);
  AnnotationValueAccess valueAccess = new AnnotationValueAccess();

  // Edm via Annotations and ListProcessor via AnnotationDS with AnnotationsValueAccess
  return RuntimeDelegate.createODataSingleProcessorService(edmProvider,
      new ListsProcessor(dataSource, valueAccess));
}
 
Example #22
Source File: ContextTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void checkContextExists() throws ClientProtocolException, IOException, ODataException {
  assertNull(getService().getProcessor().getContext());
  final HttpResponse response = executeGetRequest("$metadata");

  final ODataContext context = getService().getProcessor().getContext();
  assertNotNull(context);

  final ODataService service = context.getService();
  assertNotNull(service);

  assertEquals(Status.OK.getStatusCode(), response.getStatusLine().getStatusCode());

  assertEquals("$metadata", context.getPathInfo().getODataSegments().get(0).getPath());
}
 
Example #23
Source File: AnnotationServiceFactoryImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ODataService createAnnotationService(final String modelPackage) throws ODataException {
  AnnotationEdmProvider edmProvider = new AnnotationEdmProvider(modelPackage);
  AnnotationInMemoryDs dataSource = new AnnotationInMemoryDs(modelPackage);
  AnnotationValueAccess valueAccess = new AnnotationValueAccess();

  // Edm via Annotations and ListProcessor via AnnotationDS with AnnotationsValueAccess
  return RuntimeDelegate.createODataSingleProcessorService(edmProvider,
      new ListsProcessor(dataSource, valueAccess));
}
 
Example #24
Source File: ODataServiceFactoryWithCallbackImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataService createService(final ODataContext ctx) throws ODataException {
  return null;
}
 
Example #25
Source File: AnnotationSampleServiceFactory.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataService createService(final ODataContext context) throws ODataException {
  // Edm via Annotations and ListProcessor via AnnotationDS with AnnotationsValueAccess
  return AnnotationInstances.ANNOTATION_ODATA_SERVICE;
}
 
Example #26
Source File: RuntimeDelegate.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected abstract ODataService createODataSingleProcessorService(EdmProvider provider,
ODataSingleProcessor processor);
 
Example #27
Source File: AbstractFitTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected ODataService getService() {
  return service;
}
 
Example #28
Source File: FitStaticServiceFactory.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
public static void bindService(final String key, final ODataService service) {
  PORT_2_SERVICE.put(key, service);
}
 
Example #29
Source File: FitStaticServiceFactory.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
public static void bindService(final TestServer server, final ODataService service) {
  PORT_2_SERVICE.put(createId(server), service);
}
 
Example #30
Source File: NullServiceTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
protected ODataService createService() throws ODataException {
  return null;
}