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

The following examples show how to use org.apache.olingo.odata2.api.ODataServiceFactory. 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: 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 #2
Source File: ODataRootLocator.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
public static ODataServiceFactory createServiceFactoryFromContext(final Application app,
    final HttpServletRequest servletRequest,
    final ServletConfig servletConfig) {
  try {
    Class<?> factoryClass;
    if (app instanceof AbstractODataApplication) {
      factoryClass = ((AbstractODataApplication) app).getServiceFactoryClass();
    } else {
      final String factoryClassName = servletConfig.getInitParameter(ODataServiceFactory.FACTORY_LABEL);
      if (factoryClassName == null) {
        throw new ODataRuntimeException("Servlet config missing: " + ODataServiceFactory.FACTORY_LABEL);
      }

      ClassLoader cl = (ClassLoader) servletRequest.getAttribute(ODataServiceFactory.FACTORY_CLASSLOADER_LABEL);
      if (cl == null) {
        factoryClass = Class.forName(factoryClassName);
      } else {
        factoryClass = Class.forName(factoryClassName, true, cl);
      }
    }
    return (ODataServiceFactory) factoryClass.newInstance();
  } catch (Exception e) {
    throw new ODataRuntimeException("Exception during ODataServiceFactory creation occured.", e);
  }
}
 
Example #3
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 #4
Source File: ODataExceptionMapperImplTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void servletRequestWithClassloader() throws Exception {
  MultivaluedMap<String, String> value = new MultivaluedHashMap<String, String>();
  value.putSingle("Accept", "AcceptValue");
  value.put("AcceptMulti", Arrays.asList("AcceptValue_1", "AcceptValue_2"));
  when(exceptionMapper.httpHeaders.getRequestHeaders()).thenReturn(value);
  when(exceptionMapper.servletConfig.getInitParameter(ODataServiceFactory.FACTORY_LABEL)).thenReturn(
      ODataServiceFactoryWithCallbackImpl.class.getName());
  when(exceptionMapper.servletRequest.getAttribute(ODataServiceFactory.FACTORY_CLASSLOADER_LABEL)).thenReturn(
      ODataServiceFactoryWithCallbackImpl.class.getClassLoader());
  Response response = exceptionMapper.toResponse(new Exception());

  // verify
  assertNotNull(response);
  assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), response.getStatus());
  String errorMessage = (String) response.getEntity();
  assertEquals("bla", errorMessage);
}
 
Example #5
Source File: ODataExceptionMapperImplTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtendedODataErrorContext() throws Exception {
  MultivaluedMap<String, String> value = new MultivaluedHashMap<String, String>();
  value.putSingle("Accept", "AcceptValue");
  value.put("AcceptMulti", Arrays.asList("AcceptValue_1", "AcceptValue_2"));
  when(exceptionMapper.httpHeaders.getRequestHeaders()).thenReturn(value);
  when(exceptionMapper.servletConfig.getInitParameter(ODataServiceFactory.FACTORY_LABEL)).thenReturn(
      ODataServiceFactoryWithCallbackImpl.class.getName());
  when(exceptionMapper.servletRequest.getAttribute(ODataServiceFactory.FACTORY_CLASSLOADER_LABEL)).thenReturn(null);
  Response response = exceptionMapper.toResponse(new Exception());

  // verify
  assertNotNull(response);
  assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), response.getStatus());
  String errorMessage = (String) response.getEntity();
  assertEquals("bla", errorMessage);
  String contentTypeHeader = response.getHeaderString(org.apache.olingo.odata2.api.commons.HttpHeaders.CONTENT_TYPE);
  assertEquals("text/html", contentTypeHeader);
  //
  assertEquals(uri.toASCIIString(), response.getHeaderString("RequestUri"));
  assertEquals("[AcceptValue]", response.getHeaderString("Accept"));
  assertEquals("[AcceptValue_1, AcceptValue_2]", response.getHeaderString("AcceptMulti"));
}
 
Example #6
Source File: ODataServletTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serviceClassloader() 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");
  Mockito.when(respMock.getOutputStream()).thenReturn(Mockito.mock(ServletOutputStream.class));

  servlet.service(reqMock, respMock);

  Mockito.verify(configMock).getInitParameter(ODataServiceFactory.FACTORY_LABEL);
  Mockito.verify(reqMock).getAttribute(ODataServiceFactory.FACTORY_CLASSLOADER_LABEL);

  Assert.assertEquals(ODataServiceFactoryImpl.class, servlet.getServiceFactory(reqMock).getClass());
}
 
Example #7
Source File: ODataServletTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithNoPathAfterServletPath() throws Exception {
  ODataServlet servlet = new ODataServlet();
  prepareServlet(servlet);
  prepareRequestWithNoPathAfterServletPath(reqMock, "", "/servlet-path");
  Mockito.when(reqMock.getPathInfo()).thenReturn("/Collection");
  Mockito.when(reqMock.getRequestURI()).thenReturn("http://localhost:8080/servlet-path;v=1/Collection");
  Mockito.when(servlet.getInitParameter("org.apache.olingo.odata2.path.split")).thenReturn("1");
  Mockito.when(respMock.getOutputStream()).thenReturn(Mockito.mock(ServletOutputStream.class));

  servlet.service(reqMock, respMock);

  Mockito.verify(configMock).getInitParameter(ODataServiceFactory.FACTORY_LABEL);
  Mockito.verify(reqMock).getAttribute(ODataServiceFactory.FACTORY_CLASSLOADER_LABEL);

  Assert.assertEquals(ODataServiceFactoryImpl.class, servlet.getServiceFactory(reqMock).getClass());
}
 
Example #8
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
protected void handleRedirect(final HttpServletRequest req, final HttpServletResponse resp,
                              ODataServiceFactory serviceFactory) throws IOException {
  String method = req.getMethod();
  if (ODataHttpMethod.GET.name().equals(method) ||
      ODataHttpMethod.POST.name().equals(method) ||
      ODataHttpMethod.PUT.name().equals(method) ||
      ODataHttpMethod.DELETE.name().equals(method) ||
      ODataHttpMethod.PATCH.name().equals(method) ||
      ODataHttpMethod.MERGE.name().equals(method) ||
      HTTP_METHOD_HEAD.equals(method) ||
      HTTP_METHOD_OPTIONS.equals(method)) {
    ODataResponse odataResponse = ODataResponse.status(HttpStatusCodes.TEMPORARY_REDIRECT)
        .header(HttpHeaders.LOCATION, createLocation(req))
        .build();
    createResponse(resp, odataResponse);
  } else {
    createNotImplementedResponse(req, ODataHttpException.COMMON, resp, serviceFactory);
  }

}
 
Example #9
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private boolean handleHttpTunneling(final HttpServletRequest req, final HttpServletResponse resp,
    final String xHttpMethod, ODataServiceFactory serviceFactory) throws IOException {
  if (ODataHttpMethod.MERGE.name().equals(xHttpMethod)) {
    handleRequest(req, ODataHttpMethod.MERGE, resp, serviceFactory);
  } else if (ODataHttpMethod.PATCH.name().equals(xHttpMethod)) {
    handleRequest(req, ODataHttpMethod.PATCH, resp, serviceFactory);
  } else if (ODataHttpMethod.DELETE.name().equals(xHttpMethod)) {
    handleRequest(req, ODataHttpMethod.DELETE, resp, serviceFactory);
  } else if (ODataHttpMethod.PUT.name().equals(xHttpMethod)) {
    handleRequest(req, ODataHttpMethod.PUT, resp, serviceFactory);
  } else if (ODataHttpMethod.GET.name().equals(xHttpMethod)) {
    handleRequest(req, ODataHttpMethod.GET, resp, serviceFactory);
  } else if (HTTP_METHOD_HEAD.equals(xHttpMethod)) {
    handleRequest(req, ODataHttpMethod.GET, resp, serviceFactory);
  } else if (ODataHttpMethod.POST.name().equals(xHttpMethod)) {
    handleRequest(req, ODataHttpMethod.POST, resp, serviceFactory);
  } else if (HTTP_METHOD_OPTIONS.equals(xHttpMethod)) {
    createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp, serviceFactory);
  } else {
    createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp, serviceFactory);
  }
  return true;
}
 
Example #10
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
  // We have to create the Service Factory here because otherwise we do not have access to the error callback
  ODataServiceFactory serviceFactory = getServiceFactory(req);
  if(serviceFactory == null) {
    throw new ODataRuntimeException("Unable to get Service Factory. Check either '" +
        ODataServiceFactory.FACTORY_LABEL + "' or '" + ODataServiceFactory.FACTORY_INSTANCE_LABEL + "' config.");
  }

  String xHttpMethod = req.getHeader("X-HTTP-Method");
  String xHttpMethodOverride = req.getHeader("X-HTTP-Method-Override");
  if (xHttpMethod != null && xHttpMethodOverride != null) {
    if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) {
      ODataExceptionWrapper wrapper = new ODataExceptionWrapper(req, serviceFactory);
      createResponse(resp, wrapper.wrapInExceptionResponse(
          new ODataBadRequestException(ODataBadRequestException.AMBIGUOUS_XMETHOD)));
      return;
    }
  }

  if (req.getPathInfo() != null) {
    handle(req, resp, xHttpMethod, xHttpMethodOverride, serviceFactory);
  } else {
    handleRedirect(req, resp, serviceFactory);
  }
}
 
Example #11
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 #12
Source File: ODataServletTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void prepareServlet(final GenericServlet servlet) throws Exception {
  // private transient ServletConfig config;
  Field configField = GenericServlet.class.getDeclaredField("config");
  configField.setAccessible(true);
  configField.set(servlet, configMock);

  String factoryClassName = ODataServiceFactoryImpl.class.getName();
  Mockito.when(configMock.getInitParameter(ODataServiceFactory.FACTORY_LABEL)).thenReturn(factoryClassName);
}
 
Example #13
Source File: ODataRootLocator.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Default root behavior which will delegate all paths to a ODataLocator.
 * @param pathSegments URI path segments - all segments have to be OData
 * @param xHttpMethod HTTP Header X-HTTP-Method for tunneling through POST
 * @param xHttpMethodOverride HTTP Header X-HTTP-Method-Override for tunneling through POST
 * @return a locator handling OData protocol
 * @throws ODataException
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
@Path("/{pathSegments: .*}")
public Object handleRequest(
    @Encoded @PathParam("pathSegments") final List<PathSegment> pathSegments,
    @HeaderParam("X-HTTP-Method") final String xHttpMethod,
    @HeaderParam("X-HTTP-Method-Override") final String xHttpMethodOverride)
    throws ODataException, ClassNotFoundException, InstantiationException, IllegalAccessException {

  if (xHttpMethod != null && xHttpMethodOverride != null) {

    /*
     * X-HTTP-Method-Override : implemented by CXF
     * X-HTTP-Method : implemented in ODataSubLocator:handlePost
     */

    if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) {
      throw new ODataBadRequestException(ODataBadRequestException.AMBIGUOUS_XMETHOD);
    }
  }

  if (servletRequest.getPathInfo() == null) {
    return handleRedirect();
  }

  ODataServiceFactory serviceFactory = getServiceFactory();

  int pathSplit = getPathSplit();

  final SubLocatorParameter param = new SubLocatorParameter();
  param.setServiceFactory(serviceFactory);
  param.setPathSegments(pathSegments);
  param.setHttpHeaders(httpHeaders);
  param.setUriInfo(uriInfo);
  param.setRequest(request);
  param.setServletRequest(servletRequest);
  param.setPathSplit(pathSplit);

  return ODataSubLocator.create(param);
}
 
Example #14
Source File: ODataContextImplTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Before
public void before() {
  ODataServiceFactory factory = mock(ODataServiceFactory.class);
  ODataRequest request = mock(ODataRequest.class);

  when(request.getMethod()).thenReturn(ODataHttpMethod.GET);
  when(request.getPathInfo()).thenReturn(new PathInfoImpl());

  context = new ODataContextImpl(request, factory);
}
 
Example #15
Source File: ODataExceptionMapperImplTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCallback() throws Exception {
  when(exceptionMapper.servletConfig.getInitParameter(ODataServiceFactory.FACTORY_LABEL)).thenReturn(
      ODataServiceFactoryWithCallbackImpl.class.getName());
  Response response = exceptionMapper.toResponse(new Exception());

  // verify
  assertNotNull(response);
  assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), response.getStatus());
  String errorMessage = (String) response.getEntity();
  assertEquals("bla", errorMessage);
  String contentTypeHeader = response.getHeaderString(org.apache.olingo.odata2.api.commons.HttpHeaders.CONTENT_TYPE);
  assertEquals("text/html", contentTypeHeader);
}
 
Example #16
Source File: DispatcherTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static void checkDispatch(final ODataHttpMethod method, final UriType uriType, final boolean isValue,
    final String expectedMethodName) throws ODataException {
  ODataServiceFactory factory = mock(ODataServiceFactory.class);

  final ODataResponse response = new Dispatcher(factory, getMockService())
      .dispatch(method, mockUriInfo(uriType, isValue), null, "application/xml", "*/*");
  assertEquals(expectedMethodName, response.getEntity());
}
 
Example #17
Source File: DispatcherTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static void checkFeature(final UriType uriType, final boolean isValue,
    final Class<? extends ODataProcessor> feature) throws ODataException {
  ODataServiceFactory factory = mock(ODataServiceFactory.class);
  new Dispatcher(factory, getMockService());
  assertEquals(feature, Dispatcher.mapUriTypeToProcessorFeature(mockUriInfo(uriType, isValue)));
  assertEquals(feature, Dispatcher.mapUriTypeToProcessorFeature(mockUriInfo(uriType, isValue)));
}
 
Example #18
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 #19
Source File: ODataRootLocator.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public int getPathSplit() {
  int pathSplit = 0;
  final String pathSplitAsString = servletConfig.getInitParameter(ODataServiceFactory.PATH_SPLIT_LABEL);
  if (pathSplitAsString != null) {
    pathSplit = Integer.parseInt(pathSplitAsString);
  }
  return pathSplit;
}
 
Example #20
Source File: TestServer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public void startServer(final Class<? extends ODataServiceFactory> factoryClass, final int fixedPort) {
  try {
    final ServletContextHandler contextHandler = createContextHandler(factoryClass);
    final InetSocketAddress isa = new InetSocketAddress(DEFAULT_HOST, fixedPort);
    server = new Server(isa);

    server.setHandler(contextHandler);
    server.start();
    endpoint = new URI(DEFAULT_SCHEME, null, DEFAULT_HOST, isa.getPort(), "/abc" + path, null, null);
    log.trace("Started server at endpoint " + endpoint.toASCIIString());
  } catch (final Exception e) {
    log.error("server start failed", e);
    throw new ServerRuntimeException(e);
  }
}
 
Example #21
Source File: ODataExceptionWrapper.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private ODataErrorCallback getErrorHandlerCallbackFromContext(final ODataContext context)
    throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  ODataErrorCallback cback = null;
  ODataServiceFactory serviceFactory = context.getServiceFactory();
  cback = serviceFactory.getCallback(ODataErrorCallback.class);
  return cback;
}
 
Example #22
Source File: ODataContextImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public ODataContextImpl(final ODataRequest request, final ODataServiceFactory factory) {
  setServiceFactory(factory);
  setRequest(request);
  setPathInfo(request.getPathInfo());
  setHttpMethod(request.getHttpMethod());
  setAcceptableLanguages(request.getAcceptableLanguages());
  setDebugMode(checkDebugMode(request.getQueryParameters()));
}
 
Example #23
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Get an instance of a ODataServiceFactory from request attribute
 * ODataServiceFactory.FACTORY_INSTANCE_LABEL
 *
 * @see ODataServiceFactory#FACTORY_INSTANCE_LABEL
 *
 * @param req http servlet request
 * @return instance of a ODataServiceFactory
 */
private ODataServiceFactory getODataServiceFactoryInstance(HttpServletRequest req) {
  Object factory = req.getAttribute(ODataServiceFactory.FACTORY_INSTANCE_LABEL);
  if(factory == null) {
    return null;
  } else if(factory instanceof ODataServiceFactory) {
    return (ODataServiceFactory) factory;
  }
  throw new ODataRuntimeException("Invalid service factory instance of type " + factory.getClass());
}
 
Example #24
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create an instance of a ODataServiceFactory via factory class
 * from servlet init parameter ODataServiceFactory.FACTORY_LABEL
 * and ODataServiceFactory.FACTORY_CLASSLOADER_LABEL (if set).
 *
 * @see ODataServiceFactory#FACTORY_LABEL
 * @see ODataServiceFactory#FACTORY_CLASSLOADER_LABEL
 *
 * @param req http servlet request
 * @return instance of a ODataServiceFactory
 */
private ODataServiceFactory createODataServiceFactory(HttpServletRequest req)
    throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  final String factoryClassName = getInitParameter(ODataServiceFactory.FACTORY_LABEL);
  if(factoryClassName == null) {
    return null;
  }

  ClassLoader cl = (ClassLoader) req.getAttribute(ODataServiceFactory.FACTORY_CLASSLOADER_LABEL);
  if (cl == null) {
    return (ODataServiceFactory) Class.forName(factoryClassName).newInstance();
  } else {
    return (ODataServiceFactory) Class.forName(factoryClassName, true, cl).newInstance();
  }
}
 
Example #25
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void createServiceUnavailableResponse(HttpServletRequest req, MessageReference messageReference,
    HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException {
  ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
  ODataResponse response =
      exceptionWrapper.wrapInExceptionResponse(new ODataInternalServerErrorException(messageReference));
  createResponse(resp, response);
}
 
Example #26
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void createNotAcceptableResponse(final HttpServletRequest req, final MessageReference messageReference,
    final HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException {
  ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
  ODataResponse response =
      exceptionWrapper.wrapInExceptionResponse(new ODataNotAcceptableException(messageReference));
  createResponse(resp, response);
}
 
Example #27
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void createMethodNotAllowedResponse(final HttpServletRequest req, final MessageReference messageReference,
    final HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException {
  ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
  ODataResponse response =
      exceptionWrapper.wrapInExceptionResponse(new ODataMethodNotAllowedException(messageReference));
  createResponse(resp, response);
}
 
Example #28
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void createNotImplementedResponse(final HttpServletRequest req, final MessageReference messageReference,
    final HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException {
  // RFC 2616, 5.1.1: "An origin server SHOULD return the status code [...]
  // 501 (Not Implemented) if the method is unrecognized [...] by the origin server."
  ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
  ODataResponse response =
      exceptionWrapper.wrapInExceptionResponse(new ODataNotImplementedException(messageReference));
  createResponse(resp, response);
}
 
Example #29
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Get the service factory instance which is used for creation of the
 * <code>ODataService</code> which handles the processing of the request.
 *
 * @param request the http request which is processed as an OData request
 * @return an instance of an ODataServiceFactory
 */
protected ODataServiceFactory getServiceFactory(HttpServletRequest request) {
  try {
    ODataServiceFactory factoryInstance = getODataServiceFactoryInstance(request);
    if(factoryInstance == null) {
      return createODataServiceFactory(request);
    }
    return factoryInstance;

  } catch (Exception e) {
    throw new ODataRuntimeException(e);
  }
}
 
Example #30
Source File: ODataExceptionMapperImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private ODataErrorCallback getErrorHandlerCallback() {
  final ODataServiceFactory serviceFactory =
      ODataRootLocator.createServiceFactoryFromContext(app, servletRequest, servletConfig);
  return serviceFactory.getCallback(ODataErrorCallback.class);
}