本文整理匯總了Java中javax.ws.rs.ext.Providers類的典型用法代碼示例。如果您正苦於以下問題:Java Providers類的具體用法?Java Providers怎麽用?Java Providers使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Providers類屬於javax.ws.rs.ext包,在下文中一共展示了Providers類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: lookupResource
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
/**
* Lookup a resource of given <code>resourceType</code> type, either using a suitable {@link ContextResolver} if
* <code>providers</code> is not <code>null</code> and a {@link ContextResolver} for given <code>resourceType</code>
* is available, or trying to obtain the resource from {@link Context} using given <code>resourceType</code> as
* context resource key.
* @param <R> Resource type
* @param caller Caller class
* @param resourceType Resource type to lookup (not null)
* @param providers JAX-RS {@link Providers}, if available
* @return Resource instance, or an empty optional if not available
*/
public static <R> Optional<R> lookupResource(Class<?> caller, Class<R> resourceType, Providers providers) {
ObjectUtils.argumentNotNull(resourceType, "Resource type must be not null");
R resource = null;
// try to use a ContextResolver, if available
ContextResolver<R> resolver = providers.getContextResolver(resourceType, null);
if (resolver != null) {
resource = resolver.getContext(caller);
}
if (resource == null) {
// lookup in context
resource = Context.get()
.resource(resourceType,
(caller != null) ? caller.getClassLoader() : ClassUtils.getDefaultClassLoader())
.orElse(null);
}
return Optional.ofNullable(resource);
}
示例2: davPropFind
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
protected javax.ws.rs.core.Response davPropFind(Long id, UriInfo uriInfo, String depth, Providers providers) throws AuthenticationException, AuthorisationException,
ServiceException {
final Response folder = new Response(new HRef(uriInfo.getRequestUri()), null, null, null, new PropStat(new Prop(new DisplayName("My Collection"),
new CreationDate(new Date()), new GetLastModified(new Date()), COLLECTION), new Status((StatusType) OK)));
if (depth.equals(DEPTH_0) || id == null) {
return javax.ws.rs.core.Response.status(MULTI_STATUS).entity(new MultiStatus(folder)).build();
}
final Collection<Response> responses = new LinkedList<Response>(singletonList(folder));
Iterator<FileOutVO> it = WebUtil.getServiceLocator().getFileService().getFiles(getAuth(), getFileModule(), id, null, null, null).iterator();
if (it != null) {
while (it.hasNext()) {
FileOutVO out = it.next();
// responses.add(buildResponse(f, uriInfo.getAbsolutePathBuilder().path(format("%s", c.getMatchCode())).build(), providers));
FilePathSplitter filePath = new FilePathSplitter(out.getFileName());
String extension = filePath.getExtension();
if (isTempFile(extension) || isEditableFile(extension)) {
responses.add(buildResponse(out, uriInfo.getAbsolutePathBuilder().path(getDavFileName(out)).build(), providers));
}
}
}
return javax.ws.rs.core.Response.status(MULTI_STATUS).entity(new MultiStatus(responses.toArray(new Response[0]))).build();
}
示例3: testLocateMostRelevantConstructor
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Test
public void testLocateMostRelevantConstructor() {
ReflectionProvider rp = instance(ReflectionProvider.class);
Providers providers = instance(Providers.class);
ProviderFilter providerFilter = instance(ProviderFilter.class);
Field f = rp.getField(JaxRsResolverTest.class, "genericField3");
RuntimeDelegate.setInstance(new DefaultRuntimeDelegate());
MessageBodyReader messageBodyReader = providers.getMessageBodyReader(f.getType(), f.getGenericType(), f.getAnnotations(), MediaType.APPLICATION_XML_TYPE);
assertNotNull(messageBodyReader);
assertEquals(PersonMessageBodyReader.class, messageBodyReader.getClass());
Constructor<?> c = providerFilter.mostRelevantConstructor(messageBodyReader.getClass());
assertEquals(4, c.getParameterCount());
assertEquals("public com.geemvc.jaxrs.PersonMessageBodyReader(javax.ws.rs.core.Application,javax.ws.rs.core.UriInfo,javax.ws.rs.core.HttpHeaders,javax.ws.rs.ext.Providers)", c.toString());
}
示例4: configure
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Override
protected void configure() {
jerseyToGuice(MultivaluedParameterExtractorProvider.class);
jerseyToGuice(Application.class);
jerseyToGuice(Providers.class);
// request scoped objects, but hk will control their scope
// must be used in guice only with Provider
jerseyToGuice(UriInfo.class);
jerseyToGuice(ResourceInfo.class);
jerseyToGuice(HttpHeaders.class);
jerseyToGuice(SecurityContext.class);
jerseyToGuice(Request.class);
jerseyToGuice(ContainerRequest.class);
jerseyToGuice(AsyncContext.class);
if (!guiceServletSupport) {
// bind request and response objects when guice servlet module not registered
// but this will work only for resources
jerseyToGuice(HttpServletRequest.class);
jerseyToGuice(HttpServletResponse.class);
}
}
示例5: getContent
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
/**
* Returns the request content as the given type. IF the content is a collection, returns a list of elements of the given type
*
* @param request
* @param type
* @return
*/
public static Object getContent(byte[] raw, Providers providers, HttpHeaders httpHeaders, Class<?> type) {
try {
return getContent(new ByteArrayInputStream(raw), providers, httpHeaders, type, type, new Annotation[] {});
} catch (MinnalInstrumentationException e) {
logger.trace("Failed while getting the content from the request stream", e);
Throwable ex = e;
while (ex.getCause() != null) {
if (ex.getCause() instanceof IOException) {
return getContent(new ByteArrayInputStream(raw), providers, httpHeaders, List.class, listType(type).getType(), new Annotation[]{});
}
ex = ex.getCause();
}
throw e;
}
}
示例6: shouldGetContentAsList
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Test
public void shouldGetContentAsList() throws Exception {
List<String> list = Lists.newArrayList("test1", "test2", "test3");
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(String.class), eq(String.class), eq(new Annotation[]{}), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenThrow(IOException.class);
when(reader.readFrom(eq(List.class), any(Type.class), eq(new Annotation[]{}), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(list);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(String.class, String.class, new Annotation[]{}, mediaType)).thenReturn(reader);
when(providers.getMessageBodyReader(List.class, ResourceUtil.listType(String.class).getType(), new Annotation[]{}, mediaType)).thenReturn(reader);
Object content = ResourceUtil.getContent(bytes, providers, httpHeaders, String.class);
assertTrue(content instanceof List);
assertEquals(content, list);
}
示例7: shouldGetContentAsMap
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Test
public void shouldGetContentAsMap() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(Map.class), eq(Map.class), eq(new Annotation[]{}), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(map);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(Map.class, Map.class, new Annotation[]{}, mediaType)).thenReturn(reader);
Object content = ResourceUtil.getContent(bytes, providers, httpHeaders, Map.class);
assertTrue(content instanceof Map);
assertEquals(content, map);
}
示例8: shouldInvokeMethodWithArguments
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Test
public void shouldInvokeMethodWithArguments() throws Throwable {
Map<String, Object> content = new HashMap<String, Object>();
content.put("value", "test123");
content.put("someNumber", 1L);
Map<String, Object> values = new HashMap<String, Object>();
DummyModel model = new DummyModel();
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(Map.class), eq(Map.class), isNull(Annotation[].class), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(content);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(Map.class, Map.class, null, mediaType)).thenReturn(reader);
assertEquals(ResourceUtil.invokeAction(model, "dummyActionWithArguments", Lists.newArrayList(new ParameterMetaData("value", "value", String.class),
new ParameterMetaData("someNumber", "someNumber", Long.class)), bytes, providers, httpHeaders, values), "dummyActionWithArguments");
}
示例9: shouldInvokeMethodWithArgumentsAndModel
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Test
public void shouldInvokeMethodWithArgumentsAndModel() throws Throwable {
Map<String, Object> content = new HashMap<String, Object>();
content.put("value", "test123");
content.put("someNumber", 1L);
Map<String, Object> values = new HashMap<String, Object>();
values.put("anotherModel", new DummyModel());
DummyModel model = new DummyModel();
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(Map.class), eq(Map.class), isNull(Annotation[].class), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(content);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(Map.class, Map.class, null, mediaType)).thenReturn(reader);
assertEquals(ResourceUtil.invokeAction(model, "dummyActionWithArgumentsAndModel", Lists.newArrayList(new ParameterMetaData("anotherModel", "anotherModel", DummyModel.class),
new ParameterMetaData("value", "value", String.class), new ParameterMetaData("someNumber", "someNumber", Long.class)), bytes, providers, httpHeaders, values), "dummyActionWithArgumentsAndModel");
}
示例10: shouldThrowExceptionIfMethodNotFound
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Test(expectedExceptions=MinnalInstrumentationException.class)
public void shouldThrowExceptionIfMethodNotFound() throws Throwable {
Map<String, Object> values = new HashMap<String, Object>();
DummyModel model = new DummyModel();
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(Map.class), eq(Map.class), isNull(Annotation[].class), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(values);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(Map.class, Map.class, null, mediaType)).thenReturn(reader);
ResourceUtil.invokeAction(model, "nonExistingMethod", Lists.newArrayList(new ParameterMetaData("anotherModel", "anotherModel", DummyModel.class)), bytes, providers, httpHeaders, values);
}
示例11: shouldThrowExceptionIfMethodThrowsAnyException
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@Test(expectedExceptions=IllegalStateException.class)
public void shouldThrowExceptionIfMethodThrowsAnyException() throws Throwable {
Map<String, Object> values = new HashMap<String, Object>();
DummyModel model = new DummyModel();
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(Map.class), eq(Map.class), isNull(Annotation[].class), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(values);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(Map.class, Map.class, null, mediaType)).thenReturn(reader);
ResourceUtil.invokeAction(model, "throwsException", new ArrayList<ParameterMetaData>(), bytes, providers, httpHeaders, values);
}
示例12: contextClasses
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
private static Set<Class<?>> contextClasses() {
final Set<Class<?>> classes = new HashSet<>(); classes.add(UriInfo.class);
classes.add(SecurityContext.class);
classes.add(HttpHeaders.class);
classes.add(ContextResolver.class);
classes.add(Providers.class);
classes.add(Request.class);
/* TODO: when we have jaxrs 2
classes.add(ResourceInfo.class);
classes.add(ResourceContext.class);
*/
classes.add(Application.class);
classes.add(HttpServletRequest.class);
classes.add(HttpServletResponse.class);
classes.add(ServletConfig.class);
classes.add(ServletContext.class);
classes.add(MessageContext.class);
return classes;
}
示例13: ResourceMocker
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
/**
* Public ctor.
*/
public ResourceMocker() {
final URI home = URI.create("http://localhost:9999/local");
this.withUriInfo(new UriInfoMocker().withRequestUri(home).mock());
final HttpServletRequest request =
Mockito.mock(HttpServletRequest.class);
Mockito.doReturn(home.getHost()).when(request).getRemoteAddr();
Mockito.doReturn(home.getPath()).when(request).getRequestURI();
Mockito.doReturn(home.getPath()).when(request).getContextPath();
this.withServletRequest(request);
this.withHttpHeaders(new HttpHeadersMocker().mock());
final Providers providers = Mockito.mock(Providers.class);
Mockito.doReturn(new XslResolver())
.when(providers)
.getContextResolver(
Marshaller.class, MediaType.APPLICATION_XML_TYPE
);
this.withProviders(providers);
final SecurityContext security = Mockito.mock(SecurityContext.class);
this.withSecurityContext(security);
this.withServletContext(new MkServletContext());
}
示例14: setsAndReturnsContextElements
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
/**
* BaseResource can set and return context elements.
* @throws Exception If there is some problem inside
*/
@Test
public void setsAndReturnsContextElements() throws Exception {
final BaseResource res = new BaseResourceTest.FooResource();
final Providers provs = Mockito.mock(Providers.class);
res.setProviders(provs);
MatcherAssert.assertThat(res.providers(), Matchers.equalTo(provs));
final HttpHeaders headers = Mockito.mock(HttpHeaders.class);
res.setHttpHeaders(headers);
MatcherAssert.assertThat(res.httpHeaders(), Matchers.equalTo(headers));
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
res.setHttpServletRequest(req);
MatcherAssert.assertThat(
res.httpServletRequest(),
Matchers.equalTo(req)
);
final SecurityContext sctx = Mockito.mock(SecurityContext.class);
res.setSecurityContext(sctx);
MatcherAssert.assertThat(res.securityContext(), Matchers.equalTo(sctx));
final ServletContext vctx = Mockito.mock(ServletContext.class);
res.setServletContext(vctx);
MatcherAssert.assertThat(res.servletContext(), Matchers.equalTo(vctx));
}
示例15: createBodyPart
import javax.ws.rs.ext.Providers; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static MimeBodyPart createBodyPart(Providers providers, SMIMEOutput out) throws IOException, MessagingException {
ByteArrayOutputStream bodyOs = new ByteArrayOutputStream();
MessageBodyWriter writer = providers.getMessageBodyWriter(out.getType(), out.getGenericType(), null, out.getMediaType());
if (writer == null) {
throw new RuntimeException("Failed to find writer for type: " + out.getType().getName());
}
MultivaluedMap<String, String> bodyHeaders = new MultivaluedHashMap<String, String>();
bodyHeaders.add("Content-Type", out.getMediaType().toString());
writer.writeTo(out.getEntity(), out.getType(), out.getGenericType(), null, out.getMediaType(), bodyHeaders, bodyOs);
InternetHeaders ih = new InternetHeaders();
for (Map.Entry<String, List<String>> entry : bodyHeaders.entrySet()) {
for (String value : entry.getValue()) {
ih.addHeader(entry.getKey(), value);
}
}
return new MimeBodyPart(ih, bodyOs.toByteArray());
}