javax.activation.MimeTypeParseException Java Examples

The following examples show how to use javax.activation.MimeTypeParseException. 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: Util.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #2
Source File: Util.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #3
Source File: Util.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #4
Source File: MimeTypeMapTest.java    From knox with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypeFallback() throws MimeTypeParseException {
   MimeTypeMap<String> map = new MimeTypeMap<>();

  map.put( new MimeType( "text/xml" ), "text/xml" );
  map.put( new MimeType( "text/json" ), "text/json" );
  map.put( new MimeType( "text/*" ), "text/*" );

  map.put( new MimeType( "application/json" ), "text/json" );
  map.put( new MimeType( "application/*" ), "application/*" );

  map.put( new MimeType( "*/xml" ), "*/xml" );
  map.put( new MimeType( "*/json" ), "*/json" );
  map.put( new MimeType( "*/*" ), "*/*" );

  assertThat( map.get( "text/xml" ), is( "text/xml" ) );
  assertThat( map.get( "custom/xml" ), is( "*/xml" ) );
  assertThat( map.get( "text/custom" ), is( "text/*" ) );
  assertThat( map.get( "custom/custom" ), is( "*/*" ) );

}
 
Example #5
Source File: Util.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #6
Source File: Util.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #7
Source File: MimeType.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public MimeType(String raw) throws MimeTypeParseException {
  javax.activation.MimeType parsed = new javax.activation.MimeType(raw);
  
  this.type = parsed.getPrimaryType();
  this.subtype = parsed.getSubType();
  
  parameters = new HashMap<String,String>();
  
  MimeTypeParameterList params = parsed.getParameters();
  
  Enumeration<String> paramNames = params.getNames();
  while (paramNames.hasMoreElements()) {
    String name = paramNames.nextElement();
    System.out.println(name+"/"+params.get(name));
    parameters.put(name, params.get(name));
  }
}
 
Example #8
Source File: Util.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #9
Source File: Util.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #10
Source File: Util.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static MimeType calcExpectedMediaType(AnnotationSource primarySource,
                    ModelBuilder builder ) {
    XmlMimeType xmt = primarySource.readAnnotation(XmlMimeType.class);
    if(xmt==null)
        return null;

    try {
        return new MimeType(xmt.value());
    } catch (MimeTypeParseException e) {
        builder.reportError(new IllegalAnnotationException(
            Messages.ILLEGAL_MIME_TYPE.format(xmt.value(),e.getMessage()),
            xmt
        ));
        return null;
    }
}
 
Example #11
Source File: HasMimeTypeParameter.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Optional<MimeType> getMimeTypeFromMessage(MimeMessage message) throws MessagingException {
    try {
        return Optional.of(new MimeType(message.getContentType()));
    } catch (MimeTypeParseException e) {
        LOGGER.warn("Error while parsing message's mimeType {}", message.getContentType(), e);
        return Optional.empty();
    }
}
 
Example #12
Source File: TikaProviderTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void fromSteam() throws MimeTypeDetectionException, MimeTypeParseException, IOException {
	when(tikaInstance.detect(any(InputStream.class))).thenReturn("application/json");
	tika = new TikaProvider(tikaInstance, true);
	MimeType mimetype = tika.detect(stream);
	assertThat("detected", mimetype.match("application/json"), is(true));
}
 
Example #13
Source File: StandardDavRequest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param s mimeType as String
 * @return MimeType 
 */
private static final MimeType registerMimeType(String s) {
    try {
        return new MimeType(s);
    } catch (MimeTypeParseException e) {
        throw new RuntimeException("Can't register MIME type " + s, e);
    }
}
 
Example #14
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test(expected = ContentHandlerException.class)
public void setContent_unknown() throws ContentHandlerException, MimeTypeDetectionException, MimeTypeParseException {
	final SendGrid.Email email = new SendGrid.Email();
	final StringContent content = new StringContent(CONTENT_JSON);

	final MimeType mime = new MimeType("application/json");
	when(provider.detect(CONTENT_JSON)).thenReturn(mime);

	instance.setContent(null, email, content);
}
 
Example #15
Source File: AbstractProtocolTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Match content type against expected type
 */
protected void matchResponseContentType(String expectedType) {
	String fullType = getResponseContentType();
	if (fullType == null) {
		assertNull(expectedType);
	} else {
		try {
			MimeType ctype = new MimeType(fullType);
			assertTrue("Types "+fullType+" and "+expectedType+" do not match", ctype.match(expectedType));
		} catch (MimeTypeParseException e) {
			assertEquals(expectedType, fullType);
		}

	}
}
 
Example #16
Source File: OverrideMimetypeProvider.java    From ogham with Apache License 2.0 5 votes vote down vote up
private static MimeType toMimetype(String original, String replaced) throws MimeTypeDetectionException {
	try {
		return new MimeType(replaced);
	} catch (MimeTypeParseException e) {
		throw new InvalidMimetypeException("Replacing mimetype " + original + " by " + replaced + " failed because " + replaced + " is not valid", e);
	}
}
 
Example #17
Source File: UrlRewriteFilterDescriptorImpl.java    From knox with Apache License 2.0 5 votes vote down vote up
@Override
public UrlRewriteFilterContentDescriptor addContent( String type ) {
  UrlRewriteFilterContentDescriptorImpl impl = new UrlRewriteFilterContentDescriptorImpl();
  impl.type( type );
  contentList.add( impl );
  try {
    contentMap.put( new MimeType( type ), impl );
  } catch( MimeTypeParseException e ) {
    throw new IllegalArgumentException( type, e );
  }
  return impl;
}
 
Example #18
Source File: DocumentAbstract.java    From estatio with Apache License 2.0 5 votes vote down vote up
@Override
public TranslatableString satisfiesTranslatableSafely(final String mimeType) {
    try {
        new MimeType(mimeType);
    } catch (MimeTypeParseException e) {
        return TranslatableString.tr("Invalid mime type");
    }
    return null;
}
 
Example #19
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void setContent_html() throws ContentHandlerException, MimeTypeDetectionException, MimeTypeParseException {
	final SendGrid.Email email = new SendGrid.Email();
	final StringContent content = new StringContent(CONTENT_HTML);

	final MimeType mime = new MimeType("text/html");
	when(provider.detect(CONTENT_HTML)).thenReturn(mime);

	instance.setContent(null, email, content);

	assertEquals("The email was not correctly updated", CONTENT_HTML, email.getHtml());
}
 
Example #20
Source File: TransformerTest.java    From p3-batchrefine with Apache License 2.0 5 votes vote down vote up
protected void assertEquals(File reference, File output, MimeType content)
		throws MimeTypeParseException, IOException {
	// Plaintext matching.
	if (content.match("text/csv")) {
		assertContentEquals(reference, output);
	}

	// RDF matching.
	else if (content.match("text/turtle")
			|| content.match("application/rdf+xml")) {
		assertRDFEquals(contentsAsString(reference),
				contentsAsString(output), content.toString(),
				content.toString());
	}
}
 
Example #21
Source File: SimpleMimetypeDetectionBuilder.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Override
public MimeTypeProvider build() {
	try {
		List<MimeTypeProvider> providers = new ArrayList<>();
		buildTika(providers);
		buildDefault(providers);
		assertNotEmpty(providers);
		MimeTypeProvider provider = buildProvider(providers);
		return validateProvider(overrideProvider(provider));
	} catch (MimeTypeParseException e) {
		throw new BuildException("Failed to build mimetype provider", e);
	}
}
 
Example #22
Source File: JavaFilesProvider.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Override
public MimeType getMimeType(File file) throws MimeTypeDetectionException {
	try {
		LOG.debug("Detect mime type for file {}", file);
		String contentType = Files.probeContentType(file.toPath());
		if (contentType == null) {
			LOG.debug("Detected mime type for file {} is null", file);
			throw new MimeTypeDetectionException("Can't determine mimetype for file " + file);
		}
		return new MimeType(contentType);
	} catch (MimeTypeParseException | IOException e) {
		throw new MimeTypeDetectionException("Failed to detect mimetype for " + file, e);
	}
}
 
Example #23
Source File: RuntimeBuiltinLeafInfoImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Source parse(CharSequence text) throws SAXException  {
    try {
        if(text instanceof Base64Data)
            return new DataSourceSource( ((Base64Data)text).getDataHandler() );
        else
            return new DataSourceSource(new ByteArrayDataSource(decodeBase64(text),
                UnmarshallingContext.getInstance().getXMIMEContentType()));
    } catch (MimeTypeParseException e) {
        UnmarshallingContext.getInstance().handleError(e);
        return null;
    }
}
 
Example #24
Source File: FixedMimeTypeProvider.java    From ogham with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the provider with <code>text/plain</code> Mime Type
 */
public FixedMimeTypeProvider() {
	try {
		mimetype = new MimeType("text/plain");
	} catch (MimeTypeParseException e) {
		throw new AssertionError("This should never happen as 'text/plain' is a valid MIME type", e);
	}
}
 
Example #25
Source File: JavaActivationProvider.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Override
public MimeType getMimeType(File file) throws MimeTypeDetectionException {
	try {
		LOG.debug("Detect mime type for file {}", file);
		String contentType = map.getContentType(file);
		LOG.debug("Detected mime type for file {}: {}", file, contentType);
		return new MimeType(contentType);
	} catch (MimeTypeParseException e) {
		throw new MimeTypeDetectionException("Failed to detect mimetype for " + file, e);
	}
}
 
Example #26
Source File: TikaProvider.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Override
public MimeType detect(String content) throws MimeTypeDetectionException {
	try {
		LOG.debug("Detect mime type from stream");
		String mimetype = tika.detect(content.getBytes());
		LOG.debug("Detect mime type from stream: {}", mimetype);
		checkMimeType(mimetype);
		return new MimeType(mimetype);
	} catch (MimeTypeParseException e) {
		throw new InvalidMimetypeException("Invalid mimetype", e);
	}
}
 
Example #27
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void setContent_html() throws ContentHandlerException, MimeTypeDetectionException, MimeTypeParseException {
	final Mail email = new Mail();
	final StringContent content = new StringContent(CONTENT_HTML);

	final MimeType mime = new MimeType("text/html");
	when(provider.detect(CONTENT_HTML)).thenReturn(mime);

	instance.setContent(null, email, content);

	assertEquals("The email was not correctly updated", CONTENT_HTML, getHtml(email));
}
 
Example #28
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void setContent_text() throws ContentHandlerException, MimeTypeDetectionException, MimeTypeParseException {
	final Mail email = new Mail();

	final MimeType mime = new MimeType("text/plain");
	when(provider.detect(CONTENT_TEXT)).thenReturn(mime);

	instance.setContent(null, email, content);

	assertEquals("The email was not correctly updated", CONTENT_TEXT, getText(email));
}
 
Example #29
Source File: DataSourceSource.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public DataSourceSource(DataSource source) throws MimeTypeParseException {
    this.source = source;

    String ct = source.getContentType();
    if(ct==null) {
        charset = null;
    } else {
        MimeType mimeType = new MimeType(ct);
        this.charset = mimeType.getParameter("charset");
    }
}
 
Example #30
Source File: MimeTypeWhitelistServiceImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMimeTypeWhitelisted(final String mimeTypeString) {
	try {
		final MimeType mimeType = new MimeType(mimeTypeString);
		return isMimeTypeWhitelisted(mimeType);
	} catch (MimeTypeParseException e) {
		return false;
	}
}