本文整理汇总了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());
}