本文整理匯總了Java中org.apache.tika.mime.MediaType類的典型用法代碼示例。如果您正苦於以下問題:Java MediaType類的具體用法?Java MediaType怎麽用?Java MediaType使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MediaType類屬於org.apache.tika.mime包,在下文中一共展示了MediaType類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: findMediaType
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
/**
* Finds media type (through Apache Tika library), based on filename and magic numbers.
* @throws IOException
*/
public static MediaType findMediaType(InputStream is, String fileName) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
try {
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
md.add(Metadata.RESOURCE_NAME_KEY, fileName);
MediaType mediaType = detector.detect(bis, md);
return mediaType;
} finally {
try {
bis.close();
} catch (IOException e) {
;
}
}
}
示例2: extractContent
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
public String extractContent(File file) throws JATEException {
String content = "";
if (file == null || !file.exists()) {
throw new JATEException("File is not found!");
}
try {
String contentType = Files.probeContentType(file.toPath());
if (MediaType.TEXT_PLAIN.getBaseType().toString().equals(contentType)) {
content = parseTXTToString(file);
} else {
content = tika.parseToString(file);
}
} catch (IOException e1) {
throw new JATEException("I/O exception when detecting file type.");
} catch (TikaException tikaEx) {
throw new JATEException("Tika Content extraction exception: " + tikaEx.toString());
}
return content;
}
示例3: normalizeContentType
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
private static String normalizeContentType(String contentType) {
if (contentType == null) {
return null;
}
// URLConnection returns content/unknown as the default content-type.
if (contentType.equals("content/unknown")) {
return null;
}
if (contentType.equals(MediaType.OCTET_STREAM)) {
return null;
}
if ("xml".equals(MediaType.parse(contentType).getSubtype())) {
return null;
}
return contentType;
}
示例4: getMediaType
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
/**
* Detects the content type of the given input event. Returns
* <code>application/octet-stream</code> if the type of the event can not be
* detected.
* <p>
* It is legal for the event headers or body to be empty. The detector may
* read bytes from the start of the body stream to help in type detection.
*
* @return detected media type, or <code>application/octet-stream</code>
*/
private String getMediaType(InputStream in, Metadata metadata, boolean excludeParameters) {
MediaType mediaType;
try {
mediaType = getDetector().detect(in, metadata);
} catch (IOException e) {
throw new MorphlineRuntimeException(e);
}
String mediaTypeStr = mediaType.toString();
if (excludeParameters) {
int i = mediaTypeStr.indexOf(';');
if (i >= 0) {
mediaTypeStr = mediaTypeStr.substring(0, i);
}
}
return mediaTypeStr;
}
示例5: render
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
@Override
protected void render(RenderingContext context)
{
ContentReader contentReader = context.makeContentReader();
String sourceMimeType = contentReader.getMimetype();
// Check that Tika supports the supplied file
AutoDetectParser p = new AutoDetectParser(tikaConfig);
MediaType sourceMediaType = MediaType.parse(sourceMimeType);
if(! p.getParsers().containsKey(sourceMediaType))
{
throw new RenditionServiceException(
"Source mime type of " + sourceMimeType +
" is not supported by Tika for HTML conversions"
);
}
// Make the HTML Version using Tika
// This will also extract out any images as found
generateHTML(p, context);
}
示例6: TikaImageExtractingParser
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
private TikaImageExtractingParser(RenderingContext renderingContext) {
this.renderingContext = renderingContext;
// Our expected types
types = new HashSet<MediaType>();
types.add(MediaType.image("bmp"));
types.add(MediaType.image("gif"));
types.add(MediaType.image("jpg"));
types.add(MediaType.image("jpeg"));
types.add(MediaType.image("png"));
types.add(MediaType.image("tiff"));
// Are images going in the same place as the HTML?
if( renderingContext.getParamWithDefault(PARAM_IMAGES_SAME_FOLDER, false) )
{
RenditionLocation location = resolveRenditionLocation(
renderingContext.getSourceNode(), renderingContext.getDefinition(),
renderingContext.getDestinationNode()
);
imgFolder = location.getParentRef();
if (logger.isDebugEnabled())
{
logger.debug("Using imgFolder: " + imgFolder);
}
}
}
示例7: buildMimeTypes
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
private static ArrayList<String> buildMimeTypes(TikaConfig tikaConfig)
{
config = tikaConfig;
parser = new AutoDetectParser(config);
SUPPORTED_MIMETYPES = new ArrayList<String>();
for(MediaType mt : parser.getParsers().keySet())
{
// Add the canonical mime type
SUPPORTED_MIMETYPES.add( mt.toString() );
// And add any aliases of the mime type too - Alfresco uses some
// non canonical forms of various mimetypes, so we need all of them
for(MediaType alias : config.getMediaTypeRegistry().getAliases(mt))
{
SUPPORTED_MIMETYPES.add( alias.toString() );
}
}
return SUPPORTED_MIMETYPES;
}
示例8: testSupports
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
public void testSupports() throws Exception
{
ArrayList<String> mimeTypes = new ArrayList<String>();
for (Parser p : new Parser[] {
new OfficeParser(), new OpenDocumentParser(),
new Mp3Parser(), new OOXMLParser()
}) {
Set<MediaType> mts = p.getSupportedTypes(new ParseContext());
for (MediaType mt : mts)
{
mimeTypes.add(mt.toString());
}
}
for (String mimetype : mimeTypes)
{
boolean supports = extracter.isSupported(mimetype);
assertTrue("Mimetype should be supported: " + mimetype, supports);
}
}
示例9: testGenerateAndDeployConfig
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
@Test
public void testGenerateAndDeployConfig() throws CommandFailureException, IOException {
when(Config.mockWebServerService.getResourceTemplateMetaData(anyString(), anyString())).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"httpd.conf\"}");
CommandOutput commandOutput = mock(CommandOutput.class);
ExecReturnCode execReturnCode = mock(ExecReturnCode.class);
when(commandOutput.getReturnCode()).thenReturn(execReturnCode);
when(commandOutput.getReturnCode().wasSuccessful()).thenReturn(true);
when(Config.mockResourceService.generateResourceGroup()).thenReturn(new ResourceGroup());
when(Config.mockResourceService.generateResourceFile(anyString(), anyString(), any(ResourceGroup.class), any(), any(ResourceGeneratorType.class))).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"httpd.conf\"}");
ResourceTemplateMetaData mockMetaData = mock(ResourceTemplateMetaData.class);
when(mockMetaData.getDeployFileName()).thenReturn("httpd.conf");
when(mockMetaData.getDeployPath()).thenReturn("./anyPath");
when(mockMetaData.getContentType()).thenReturn(MediaType.TEXT_PLAIN);
when(Config.mockResourceService.getTokenizedMetaData(anyString(), Matchers.anyObject(), anyString())).thenReturn(mockMetaData);
Response response = webServerServiceRest.generateAndDeployConfig(webServer.getName(), "httpd.conf", Config.mockAuthenticatedUser);
assertTrue(response.hasEntity());
}
示例10: testGenerateAndDeployConfigBinaryFile
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
@Test
public void testGenerateAndDeployConfigBinaryFile() throws CommandFailureException, IOException {
when(Config.mockWebServerService.getResourceTemplateMetaData(anyString(), anyString())).thenReturn("{\"contentType\":\"application/binary\",\"deployPath\":\"./anyPath\"}");
when(Config.mockResourceService.generateResourceGroup()).thenReturn(new ResourceGroup());
CommandOutput commandOutput = mock(CommandOutput.class);
ExecReturnCode execReturnCode = mock(ExecReturnCode.class);
when(commandOutput.getReturnCode()).thenReturn(execReturnCode);
when(commandOutput.getReturnCode().wasSuccessful()).thenReturn(true);
when(Config.mockResourceService.generateResourceFile(anyString(), anyString(), any(ResourceGroup.class), any(), any(ResourceGeneratorType.class))).thenReturn("{\"contentType\":\"application/binary\",\"deployPath\":\"./anyPath\"}");
ResourceTemplateMetaData mockMetaData = mock(ResourceTemplateMetaData.class);
when(mockMetaData.getDeployFileName()).thenReturn("httpd.conf");
when(mockMetaData.getDeployPath()).thenReturn("./anyPath");
when(mockMetaData.getContentType()).thenReturn(MediaType.TEXT_PLAIN);
when(Config.mockResourceService.getTokenizedMetaData(anyString(), Matchers.anyObject(), anyString())).thenReturn(mockMetaData);
Response response = webServerServiceRest.generateAndDeployConfig(webServer.getName(), "httpd.exe", Config.mockAuthenticatedUser);
assertTrue(response.hasEntity());
}
示例11: testGenerateAndDeployWebServerCallInstallServiceWSFails
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
@Test(expected = InternalErrorException.class)
public void testGenerateAndDeployWebServerCallInstallServiceWSFails() throws CommandFailureException, IOException {
List<String> webServerResourceNames = new ArrayList<>();
webServerResourceNames.add("httpd.conf");
CommandOutput retSuccessExecData = new CommandOutput(new ExecReturnCode(0), "", "");
CommandOutput retFailExecData = new CommandOutput(new ExecReturnCode(1), "", "Failed secure copy");
when(Config.mockWebServerControlService.controlWebServer(any(ControlWebServerRequest.class), any(User.class))).thenReturn(retSuccessExecData);
when(Config.mockWebServerControlService.controlWebServer(eq(new ControlWebServerRequest(webServer.getId(), WebServerControlOperation.INSTALL_SERVICE)), any(User.class))).thenReturn(retFailExecData);
when(Config.mockWebServerService.getWebServer(anyString())).thenReturn(webServer);
when(Config.mockWebServerService.generateInstallServiceScript(any(WebServer.class))).thenReturn("invoke me");
when(Config.mockWebServerService.getResourceTemplateMetaData(anyString(), anyString())).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"httpd.conf\"}");
when(Config.mockWebServerService.getResourceTemplateNames(anyString())).thenReturn(webServerResourceNames);
when(Config.mockResourceService.generateResourceGroup()).thenReturn(new ResourceGroup());
when(Config.mockResourceService.generateResourceFile(anyString(), anyString(), any(ResourceGroup.class), any(), any(ResourceGeneratorType.class))).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"httpd.conf\"}");
ResourceTemplateMetaData mockMetaData = mock(ResourceTemplateMetaData.class);
when(mockMetaData.getDeployFileName()).thenReturn("httpd.conf");
when(mockMetaData.getDeployPath()).thenReturn("./anyPath");
when(mockMetaData.getContentType()).thenReturn(MediaType.TEXT_PLAIN);
when(Config.mockResourceService.getTokenizedMetaData(anyString(), Matchers.anyObject(), anyString())).thenReturn(mockMetaData);
webServerServiceRest.generateAndDeployWebServer(webServer.getName(), Config.mockAuthenticatedUser);
}
示例12: testGenerateAndDeployWebServerTemplate
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
@Test
public void testGenerateAndDeployWebServerTemplate() throws CommandFailureException, IOException {
CommandOutput retSuccessExecData = new CommandOutput(new ExecReturnCode(0), "", "");
when(Config.mockWebServerService.getResourceTemplateMetaData(anyString(), anyString())).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"${webServer.name}.txt\"}");
CommandOutput commandOutput = mock(CommandOutput.class);
ExecReturnCode execReturnCode = mock(ExecReturnCode.class);
when(commandOutput.getReturnCode()).thenReturn(execReturnCode);
when(commandOutput.getReturnCode().wasSuccessful()).thenReturn(true);
when(Config.mockResourceService.generateResourceGroup()).thenReturn(new ResourceGroup());
when(Config.mockWebServerService.getResourceTemplate(anyString(), anyString(), anyBoolean(), any(ResourceGroup.class))).thenReturn("");
when(Config.mockWebServerService.getWebServer(anyString())).thenReturn(webServer);
when(Config.mockResourceService.generateResourceFile(anyString(), anyString(), any(ResourceGroup.class), any(), any(ResourceGeneratorType.class))).thenReturn("{\"contentType\":\"text/plain\",\"deployPath\":\"./anyPath\",\"deployFileName\":\"${webServer.name}.txt\"}");
ResourceTemplateMetaData mockMetaData = mock(ResourceTemplateMetaData.class);
when(mockMetaData.getDeployFileName()).thenReturn("httpd.conf");
when(mockMetaData.getDeployPath()).thenReturn("./anyPath");
when(mockMetaData.getContentType()).thenReturn(MediaType.TEXT_PLAIN);
when(Config.mockResourceService.getTokenizedMetaData(anyString(), Matchers.anyObject(), anyString())).thenReturn(mockMetaData);
Response response = webServerServiceRest.generateAndDeployConfig(webServer.getName(), "${webServer.name}.txt", Config.mockAuthenticatedUser);
assertTrue(response.hasEntity());
}
示例13: createResource
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
@Override
@Transactional
public CreateResourceResponseWrapper createResource(final ResourceIdentifier resourceIdentifier,
final ResourceTemplateMetaData metaData,
final InputStream templateData) {
String templateContent;
if (MEDIA_TYPE_TEXT.equalsIgnoreCase(metaData.getContentType().getType()) || metaData.getContentType().equals(MediaType.APPLICATION_XML)) {
Scanner scanner = new Scanner(templateData).useDelimiter("\\A");
templateContent = scanner.hasNext() ? scanner.next() : "";
} else {
templateContent = uploadResource(metaData, templateData);
}
LOGGER.debug("Creating resource content for {} :: Content={}", resourceIdentifier, templateContent);
try {
return resourceHandler.createResource(resourceIdentifier, metaData, templateContent);
} catch (final ResourceServiceException rhe) {
throw new ResourceServiceException(rhe);
}
}
示例14: ResourceTemplateMetaData
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
@JsonCreator
public ResourceTemplateMetaData(@JsonProperty("templateName") final String templateName,
@JsonProperty("contentType") final MediaType contentType,
@JsonProperty("deployFileName") final String deployFileName,
@JsonProperty("deployPath") final String deployPath,
@JsonProperty("entity") final Entity entity,
@JsonProperty("unpack") final Boolean unpack,
@JsonProperty("overwrite") Boolean overwrite,
@JsonProperty("hotDeploy") Boolean hotDeploy) {
this.templateName = templateName;
this.contentType = contentType;
this.deployFileName = deployFileName;
this.deployPath = deployPath;
this.entity = entity;
this.unpack = unpack == null ? false : unpack;
this.overwrite = overwrite == null ? true : overwrite;
this.hotDeploy = hotDeploy == null ? false : hotDeploy;
}
示例15: findMimeTypeLib
import org.apache.tika.mime.MediaType; //導入依賴的package包/類
/**
* Finds MimeType (through Apache Tika library), as string representation, based on filename and magic numbers.
*/
protected GenericValue findMimeTypeLib(ByteBuffer byteBuffer, String fileName) throws GeneralException {
MediaType mediaType = TikaUtil.findMediaTypeSafe(byteBuffer, fileName);
if (mediaType != null) {
String mimeTypeId = TikaUtil.getMimeTypeId(mediaType);
// SPECIAL: here we assume Tika identified the file correctly.
// so if it maps to a non-allowed type, we must throw error, NOT return null.
if (!isAllowedMediaType(mimeTypeId)) {
throw new FileTypeException(PropertyMessage.make("CMSErrorUiLabels", "CmsInvalidFileTypeForMediaCat",
UtilMisc.toMap("mediaType", mimeTypeId, "providedType", getProvidedType())));
}
}
// check if we have an exact match in system
// NOTE: is possible there is no exact match in the system (though should try to minimize this)
return TikaUtil.findEntityMimeTypeForMediaType(delegator, mediaType, true);
}