Java Code Examples for javax.ws.rs.ext.RuntimeDelegate#setInstance()

The following examples show how to use javax.ws.rs.ext.RuntimeDelegate#setInstance() . 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: Activator.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
@Override
public void start(BundleContext context) throws Exception {
	// Ensure that the service loader uses the right class
	// See: http://www.mail-archive.com/[email protected]/msg07539.html
	ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
	ClassLoader newcl = RuntimeDelegate.class.getClassLoader();
	
	Thread.currentThread().setContextClassLoader(newcl);
	try {
		RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
	} finally {
		Thread.currentThread().setContextClassLoader(oldcl);
	}
	super.start(context);
}
 
Example 2
Source File: HeaderHelperTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void transformsObjectToStringWithToStringMethodWhenHeaderDelegateDoesNotPresent() throws Exception {
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    RuntimeDelegate.setInstance(runtimeDelegate);

    Object someObject = mock(Object.class);
    when(someObject.toString()).thenReturn("<foo>");

    assertEquals("<foo>", HeaderHelper.getHeaderAsString(someObject));
}
 
Example 3
Source File: HeaderHelperTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void transformsObjectToStringWithHeaderDelegate() throws Exception {
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    RuntimeDelegate.setInstance(runtimeDelegate);

    HeaderDelegate<String> headerDelegate = mock(HeaderDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(String.class)).thenReturn(headerDelegate);
    when(headerDelegate.toString("foo")).thenReturn("<foo>");

    assertEquals("<foo>", HeaderHelper.getHeaderAsString("foo"));
}
 
Example 4
Source File: ResponseImplTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void getMultipleHeaderAsStringAndUsesRuntimeDelegateForConvertValuesToString() throws Exception {
    HeaderDelegate<HeaderValue> headerDelegate = mock(HeaderDelegate.class);
    when(headerDelegate.toString(isA(HeaderValue.class))).thenReturn("bar1", "bar2");
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(HeaderValue.class)).thenReturn(headerDelegate);
    RuntimeDelegate.setInstance(runtimeDelegate);

    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put("foo", newArrayList(new HeaderValue(), new HeaderValue()));
    ResponseImpl response = new ResponseImpl(200, "foo", null, headers);

    assertEquals("bar1,bar2", response.getHeaderString("foo"));
}
 
Example 5
Source File: EverrestGuiceContextListener.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServletContext servletContext = sce.getServletContext();
    ResourceBinderImpl resources = new ResourceBinderImpl();
    ApplicationProviderBinder providers = new ApplicationProviderBinder();
    Injector injector = getInjector(servletContext);
    DependencySupplier dependencySupplier = new GuiceDependencySupplier(injector);
    EverrestConfiguration config = injector.getInstance(EverrestConfiguration.class);
    EverrestServletContextInitializer everrestInitializer = new EverrestServletContextInitializer(servletContext);
    Application application = everrestInitializer.getApplication();
    EverrestApplication everrest = new EverrestApplication();
    if (config.isAsynchronousSupported()) {
        everrest.addResource(config.getAsynchronousServicePath(), AsynchronousJobService.class);
        everrest.addSingleton(new AsynchronousJobPool(config));
        everrest.addSingleton(new AsynchronousProcessListWriter());
    }
    if (config.isCheckSecurity()) {
        everrest.addSingleton(new SecurityConstraint());
    }
    everrest.addApplication(application);

    processBindings(injector, everrest);
    RequestDispatcher requestDispatcher = new RequestDispatcher(resources);
    RequestHandlerImpl requestHandler = new RequestHandlerImpl(requestDispatcher, providers);
    EverrestProcessor processor = new EverrestProcessor(config, dependencySupplier, requestHandler, everrest);
    processor.start();

    servletContext.setAttribute(EverrestConfiguration.class.getName(), config);
    servletContext.setAttribute(Application.class.getName(), everrest);
    servletContext.setAttribute(DependencySupplier.class.getName(), dependencySupplier);
    servletContext.setAttribute(ResourceBinder.class.getName(), resources);
    servletContext.setAttribute(ApplicationProviderBinder.class.getName(), providers);
    servletContext.setAttribute(EverrestProcessor.class.getName(), processor);
    // use specific RuntimeDelegate instance which is able to work with guice rest service proxies.
    // (need for interceptors functionality)
    RuntimeDelegate.setInstance(new GuiceRuntimeDelegateImpl());
}
 
Example 6
Source File: ResponseImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static final void assertNotStringBeanRuntimeDelegate(RuntimeDelegate delegate) {
    if (delegate instanceof StringBeanRuntimeDelegate) {
        StringBeanRuntimeDelegate sbrd = (StringBeanRuntimeDelegate) delegate;
        if (sbrd.getOriginal() != null) {
            RuntimeDelegate.setInstance(sbrd.getOriginal());
            throw new RuntimeException(
                "RuntimeDelegate.getInstance() is StringBeanRuntimeDelegate");
        }
    }
}
 
Example 7
Source File: ResponseImplTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void getSingleHeaderAsStringAndUsesRuntimeDelegateForConvertValueToString() throws Exception {
    HeaderDelegate<HeaderValue> headerDelegate = mock(HeaderDelegate.class);
    when(headerDelegate.toString(isA(HeaderValue.class))).thenReturn("bar");
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(HeaderValue.class)).thenReturn(headerDelegate);
    RuntimeDelegate.setInstance(runtimeDelegate);

    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put("foo", newArrayList(new HeaderValue()));
    ResponseImpl response = new ResponseImpl(200, "foo", null, headers);

    assertEquals("bar", response.getHeaderString("foo"));
}
 
Example 8
Source File: DSSSpringApplication.java    From DataSphereStudio with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    RuntimeDelegate.setInstance(new org.glassfish.jersey.internal.RuntimeDelegateImpl());
    final SpringApplication application = new SpringApplication(DSSSpringApplication.class);
    application.addListeners(new ApplicationListener<ApplicationPreparedEvent>(){
        public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent) {
            logger.info("add config from config server...");
            if(applicationContext == null) {
                applicationContext = applicationPreparedEvent.getApplicationContext();
                try {
                    setApplicationContext(applicationContext);
                } catch (Exception e) {
                    logger.error(e);
                }
            }
            addRemoteConfig();
            logger.info("initialize DataWorkCloud spring application...");
            initDWCApplication();
        }
    });
    application.addListeners(new ApplicationListener<RefreshScopeRefreshedEvent>() {
        public void onApplicationEvent(RefreshScopeRefreshedEvent applicationEvent) {
            logger.info("refresh config from config server...");
            updateRemoteConfig();
        }
    });
    String listeners = ServerConfiguration.BDP_SERVER_SPRING_APPLICATION_LISTENERS().getValue();
    if(StringUtils.isNotBlank(listeners)) {
        for (String listener : listeners.split(",")) {
            application.addListeners((ApplicationListener<?>) Class.forName(listener).newInstance());
        }
    }
    applicationContext = application.run(args);
    setApplicationContext(applicationContext);
}
 
Example 9
Source File: Activator.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
@Override
public void start(BundleContext context) throws Exception {
	// Ensure that the service loader uses the right class
	// See: http://www.mail-archive.com/[email protected]/msg07539.html
	ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
	ClassLoader newcl = RuntimeDelegate.class.getClassLoader();
	
	Thread.currentThread().setContextClassLoader(newcl);
	try {
		RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
	} finally {
		Thread.currentThread().setContextClassLoader(oldcl);
	}
	super.start(context);
}
 
Example 10
Source File: AcceptLanguageTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    headerDelegate = mock(RuntimeDelegate.HeaderDelegate.class);

    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(AcceptLanguage.class)).thenReturn(headerDelegate);

    RuntimeDelegate.setInstance(runtimeDelegate);
}
 
Example 11
Source File: RangesTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    headerDelegate = mock(HeaderDelegate.class);

    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(Ranges.class)).thenReturn(headerDelegate);

    RuntimeDelegate.setInstance(runtimeDelegate);
}
 
Example 12
Source File: ResponseImplTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void getsHeadersAsStringToStringMapAndUsesRuntimeDelegateForConvertValuesToString() throws Exception {
    HeaderDelegate<HeaderValue> headerDelegate = mock(HeaderDelegate.class);
    when(headerDelegate.toString(isA(HeaderValue.class))).thenReturn("bar");
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(HeaderValue.class)).thenReturn(headerDelegate);
    RuntimeDelegate.setInstance(runtimeDelegate);

    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put("foo", newArrayList(new HeaderValue()));
    ResponseImpl response = new ResponseImpl(200, "foo", null, headers);

    assertEquals(ImmutableMap.of("foo", newArrayList("bar")), response.getStringHeaders());
}
 
Example 13
Source File: CxfJaxrsBundleActivator.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public void stop(BundleContext context) throws Exception {
    if (_log.isDebugEnabled()) {
        _log.debug("Stopping whiteboard factory");
    }

    _defaultOSGiResult.close();

    if (_log.isDebugEnabled()) {
        _log.debug("Stopped whiteboard factory");
    }

    RuntimeDelegate.setInstance(null);
}
 
Example 14
Source File: ExceptionsHandlerTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setUp() {
    tested = new ExceptionsHandler();
    RuntimeDelegate.setInstance(runtimeDelegate);
    when(runtimeDelegate.createResponseBuilder()).thenReturn(builder);
    when(builder.status(any(Response.StatusType.class))).thenReturn(builder);
    when(builder.header(anyString(), any())).thenReturn(builder);
    when(builder.entity(any())).thenReturn(builder);
    when(builder.build()).thenReturn(response);
}
 
Example 15
Source File: ComponentContainerImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public ComponentContainerImpl() {
  // Register RESTEasy with JAX-RS as early as possible
  RuntimeDelegate.setInstance(checkNotNull(deployment.getProviderFactory()));
}
 
Example 16
Source File: Activator.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public void stop(BundleContext context) throws Exception {
    RuntimeDelegate.setInstance(null);
}
 
Example 17
Source File: OnPremisesIdeServletContextListener.java    From codenvy with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
  RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
}
 
Example 18
Source File: RangesTest.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@After
public void tearDown() throws Exception {
    RuntimeDelegate.setInstance(null);
}
 
Example 19
Source File: ResponseImplTest.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@After
public void tearDown() throws Exception {
    RuntimeDelegate.setInstance(null);
}
 
Example 20
Source File: AcceptLanguageTest.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@After
public void tearDown() throws Exception {
    RuntimeDelegate.setInstance(null);
}