本文整理汇总了Java中javax.ws.rs.core.MediaType.valueOf方法的典型用法代码示例。如果您正苦于以下问题:Java MediaType.valueOf方法的具体用法?Java MediaType.valueOf怎么用?Java MediaType.valueOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.ws.rs.core.MediaType
的用法示例。
在下文中一共展示了MediaType.valueOf方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: get_GetBreweryCollection_ThroughLdApi
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@Test
public void get_GetBreweryCollection_ThroughLdApi() {
// Arrange
Model model = new ModelBuilder().subject(DBEERPEDIA.BREWERIES).add(RDFS.LABEL,
DBEERPEDIA.BREWERIES_LABEL).build();
SparqlHttpStub.returnGraph(model);
MediaType mediaType = MediaType.valueOf("text/turtle");
// Act
Response response = target.path("/dbp/ld/v1/graph-breweries").request().accept(mediaType).get();
// Assert
assertThat(response.getStatus(), equalTo(Status.OK.getStatusCode()));
assertThat(response.getMediaType(), equalTo(mediaType));
assertThat(response.getLength(), greaterThan(0));
assertThat(response.readEntity(String.class),
containsString(DBEERPEDIA.BREWERIES_LABEL.stringValue()));
}
示例2: get_GetDocResource_ThroughLdApi
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@Test
public void get_GetDocResource_ThroughLdApi() {
// Arrange
Model model = new ModelBuilder().subject(DBEERPEDIA.BREWERIES).add(RDFS.LABEL,
DBEERPEDIA.BREWERIES_LABEL).build();
SparqlHttpStub.returnGraph(model);
MediaType mediaType = MediaType.valueOf("text/turtle");
// Act
Response response = target.path("/dbp/ld/v1/doc/breweries").request().accept(mediaType).get();
// Assert
assertThat(response.getStatus(), equalTo(Status.OK.getStatusCode()));
assertThat(response.getMediaType(), equalTo(mediaType));
assertThat(response.getLength(), greaterThan(0));
assertThat(response.readEntity(String.class),
containsString(DBEERPEDIA.BREWERIES_LABEL.stringValue()));
}
示例3: getResolver
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
private Resolver<T> getResolver(final RoutingContext context,
final Epsilon<T> income) {
/** 1.Read the resolver first **/
final Annotation annotation = income.getAnnotation();
final Class<?> resolverCls = Instance.invoke(annotation, "resolver");
/** 2.Check configured in default **/
if (null == resolverCls) {
/** 3. Old path **/
final JsonObject content = NODE.read();
final String header = context.request().getHeader(HttpHeaders.CONTENT_TYPE);
final String resolver;
if (null == header) {
resolver = content.getString("default");
} else {
final MediaType type = MediaType.valueOf(header);
final JsonObject resolverMap = content.getJsonObject(type.getType());
resolver = resolverMap.getString(type.getSubtype());
}
LOGGER.info(Info.RESOLVER, resolver, header, context.request().absoluteURI());
return Instance.singleton(resolver);
} else {
LOGGER.info(Info.RESOLVER_CONFIG, resolverCls);
return Instance.singleton(resolverCls);
}
}
示例4: invoke
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@Override
Object invoke(final MinijaxRequestContext ctx) throws Exception {
final String resourceName;
if (directory) {
resourceName = baseResourceName + "/" + ctx.getUriInfo().getPathParameters().getFirst("file");
} else {
resourceName = baseResourceName;
}
final URL resourceUrl = MinijaxStaticResource.class.getClassLoader().getResource(resourceName);
if (resourceUrl == null) {
return Response.status(Status.NOT_FOUND).build();
}
final File resourceFile = new File(resourceUrl.getFile());
if (resourceFile.isDirectory()) {
return Response.status(Status.NOT_FOUND).build();
}
final String contentType = Files.probeContentType(resourceFile.toPath());
final MediaType mediaType = contentType == null ? MediaType.APPLICATION_OCTET_STREAM_TYPE : MediaType.valueOf(contentType);
return Response.ok(resourceUrl.openStream(), mediaType).cacheControl(PUBLIC).build();
}
示例5: jsonParse
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
static Map<?, ?> jsonParse(final HttpURLConnection c, final boolean useErrorStream
) throws IOException {
if (c.getContentLength() == 0) {
return null;
}
final InputStream in = useErrorStream? c.getErrorStream(): c.getInputStream();
if (in == null) {
throw new IOException("The " + (useErrorStream? "error": "input") + " stream is null.");
}
try {
final String contentType = c.getContentType();
if (contentType != null) {
final MediaType parsed = MediaType.valueOf(contentType);
if (!MediaType.APPLICATION_JSON_TYPE.isCompatible(parsed)) {
throw new IOException("Content-Type \"" + contentType
+ "\" is incompatible with \"" + MediaType.APPLICATION_JSON
+ "\" (parsed=\"" + parsed + "\")");
}
}
ObjectMapper mapper = new ObjectMapper();
return mapper.reader(Map.class).readValue(in);
} finally {
in.close();
}
}
示例6: update
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@PUT
@Path(value = "/{id}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void update(@NotNull @PathParam("id") @ApiParam(required = true) String id, @MultipartForm ConnectorFormData connectorFormData) {
if (connectorFormData.getConnector() == null) {
throw new IllegalArgumentException("Missing connector parameter");
}
Connector connectorToUpdate = connectorFormData.getConnector();
if (connectorFormData.getIconInputStream() != null) {
try(BufferedInputStream iconStream = new BufferedInputStream(connectorFormData.getIconInputStream())) {
// URLConnection.guessContentTypeFromStream resets the stream after inspecting the media type so
// can continue to be used, rather than being consumed.
String guessedMediaType = URLConnection.guessContentTypeFromStream(iconStream);
if (!guessedMediaType.startsWith("image/")) {
throw new IllegalArgumentException("Invalid file contents for an image");
}
MediaType mediaType = MediaType.valueOf(guessedMediaType);
Icon.Builder iconBuilder = new Icon.Builder().mediaType(mediaType.toString());
Icon icon = getDataManager().create(iconBuilder.build());
iconDao.write(icon.getId().get(), iconStream);
connectorToUpdate = connectorToUpdate.builder().icon("db:" + icon.getId().get()).build();
} catch (IOException e) {
throw new IllegalArgumentException("Error while reading multipart request", e);
}
}
getDataManager().update(connectorToUpdate);
}
示例7: VertxRoutingContextHttpHeaders
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
public VertxRoutingContextHttpHeaders(final RoutingContext context) {
this.context = context;
final String obj = getHeaderString(CONTENT_TYPE);
if (obj == null) {
mediaType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
} else {
mediaType = MediaType.valueOf(obj);
}
}
示例8: getEntity
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@Override
public Entity getEntity(List<MultiPartItem> items, String mediaType) {
MultiPart multiPart = new MultiPart();
for (MultiPartItem item : items) {
if (item.getValue() == null || item.getValue().isNull()) {
continue;
}
String name = item.getName();
String filename = item.getFilename();
ScriptValue sv = item.getValue();
String ct = item.getContentType();
if (ct == null) {
ct = HttpUtils.getContentType(sv);
}
MediaType itemType = MediaType.valueOf(ct);
if (name == null) { // most likely multipart/mixed
BodyPart bp = new BodyPart().entity(sv.getAsString()).type(itemType);
multiPart.bodyPart(bp);
} else if (filename != null) {
StreamDataBodyPart part = new StreamDataBodyPart(name, sv.getAsStream(), filename, itemType);
multiPart.bodyPart(part);
} else {
multiPart.bodyPart(new FormDataBodyPart(name, sv.getAsString(), itemType));
}
}
return Entity.entity(multiPart, mediaType);
}
示例9: getMediaType
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
private MediaType getMediaType() {
final List<String> contentType = headers.get(HttpHeaders.CONTENT_TYPE);
if (contentType == null || contentType.isEmpty()) {
return null;
}
return MediaType.valueOf(contentType.get(0));
}
示例10: create
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@POST
@ApiOperation("Updates the connector icon for the specified connector and returns the updated connector")
@ApiResponses(@ApiResponse(code = 200, response = Connector.class, message = "Updated Connector icon"))
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Connector create(MultipartFormDataInput dataInput) {
if (dataInput == null || dataInput.getParts() == null || dataInput.getParts().isEmpty()) {
throw new IllegalArgumentException("Multipart request is empty");
}
if (dataInput.getParts().size() != 1) {
throw new IllegalArgumentException("Wrong number of parts in multipart request");
}
try {
InputPart filePart = dataInput.getParts().iterator().next();
InputStream result = filePart.getBody(InputStream.class, null);
if (result == null) {
throw new IllegalArgumentException("Can't find a valid 'icon' part in the multipart request");
}
try (BufferedInputStream iconStream = new BufferedInputStream(result)) {
MediaType mediaType = filePart.getMediaType();
if (!mediaType.getType().equals("image")) {
// URLConnection.guessContentTypeFromStream resets the stream after inspecting the media type so
// can continue to be used, rather than being consumed.
String guessedMediaType = URLConnection.guessContentTypeFromStream(iconStream);
if (!guessedMediaType.startsWith("image/")) {
throw new IllegalArgumentException("Invalid file contents for an image");
}
mediaType = MediaType.valueOf(guessedMediaType);
}
Icon.Builder iconBuilder = new Icon.Builder()
.mediaType(mediaType.toString());
Icon icon;
String connectorIcon = connector.getIcon();
if (connectorIcon != null && connectorIcon.startsWith("db:")) {
String connectorIconId = connectorIcon.substring(3);
iconBuilder.id(connectorIconId);
icon = iconBuilder.build();
getDataManager().update(icon);
} else {
icon = getDataManager().create(iconBuilder.build());
}
iconDao.write(icon.getId().get(), iconStream);
Connector updatedConnector = new Connector.Builder().createFrom(connector).icon("db:" + icon.getId().get()).build();
getDataManager().update(updatedConnector);
return updatedConnector;
}
} catch (IOException e) {
throw new IllegalArgumentException("Error while reading multipart request", e);
}
}
示例11: create
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation("Creates a new Connector based on the ConnectorTemplate identified by the provided `id` and the data given in `connectorSettings` multipart part, plus optional `icon` file")
@ApiResponses(@ApiResponse(code = 200, response = Connector.class, message = "Newly created Connector"))
public Connector create(@MultipartForm CustomConnectorFormData customConnectorFormData) throws IOException {
final ConnectorSettings connectorSettings = customConnectorFormData.getConnectorSettings();
if (connectorSettings == null) {
throw new IllegalArgumentException("Missing connectorSettings parameter");
}
final ConnectorSettings connectorSettingsToUse;
if (connectorSettings.getConfiguredProperties().containsKey("specification")) {
connectorSettingsToUse = connectorSettings;
} else {
final String specification;
try (BufferedSource source = Okio.buffer(Okio.source(customConnectorFormData.getSpecification()))) {
specification = source.readUtf8();
}
connectorSettingsToUse = new ConnectorSettings.Builder().createFrom(connectorSettings).putConfiguredProperty("specification", specification).build();
}
Connector generatedConnector = withGeneratorAndTemplate(connectorSettingsToUse.getConnectorTemplateId(),
(generator, template) -> generator.generate(template, connectorSettingsToUse));
if (customConnectorFormData.getIconInputStream() != null) {
// URLConnection.guessContentTypeFromStream resets the stream after inspecting the media type so
// can continue to be used, rather than being consumed.
try(BufferedInputStream iconStream = new BufferedInputStream(customConnectorFormData.getIconInputStream())) {
String guessedMediaType = URLConnection.guessContentTypeFromStream(iconStream);
if (!guessedMediaType.startsWith("image/")) {
throw new IllegalArgumentException("Invalid file contents for an image");
}
MediaType mediaType = MediaType.valueOf(guessedMediaType);
Icon.Builder iconBuilder = new Icon.Builder()
.mediaType(mediaType.toString());
Icon icon = getDataManager().create(iconBuilder.build());
iconDao.write(icon.getId().get(), iconStream);
generatedConnector = new Connector.Builder().createFrom(generatedConnector).icon("db:" + icon.getId().get()).build();
} catch (IOException e) {
throw new IllegalArgumentException("Error while reading multipart request", e);
}
}
return getDataManager().create(generatedConnector);
}
示例12: getMediaType
import javax.ws.rs.core.MediaType; //导入方法依赖的package包/类
@Override
public MediaType getMediaType() {
final String contentType = getHeaderString("Content-Type");
return contentType == null ? null : MediaType.valueOf(contentType);
}