org.restlet.data.Preference Java Examples

The following examples show how to use org.restlet.data.Preference. 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: BundleHeaderResource.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Representation get(final Variant variant) {
	try {
		final List<Preference<Language>> acceptedLanguages = getClientInfo()
				.getAcceptedLanguages();

		final String locale = acceptedLanguages == null
				|| acceptedLanguages.isEmpty() ? null : acceptedLanguages
				.get(0).getMetadata().toString();

		final org.osgi.framework.Bundle bundle = getBundleFromKeys(RestService.BUNDLE_ID_KEY);
		if (bundle == null) {
			return ERROR(Status.CLIENT_ERROR_NOT_FOUND);
		}
		return getRepresentation(
				mapFromDict(locale == null ? bundle.getHeaders()
						: bundle.getHeaders(locale)), variant);
	} catch (final Exception e) {
		return ERROR(e, variant);
	}
}
 
Example #2
Source File: AbstractOntopiaResource.java    From ontopia with Apache License 2.0 6 votes vote down vote up
/**
 * Blocks the use of specified mime types for this resource, as it is known that the converter for that mime type
 * cannot produce the representation for the Resource's target class.
 * @param types The mime types to block
 */
protected void blockMimeType(MediaType... types) {
	List<Preference<MediaType>> acceptedMediaTypes = getClientInfo().getAcceptedMediaTypes();
	if (acceptedMediaTypes.size() > types.length) {
		return;
	}	

	Set<MediaType> accepted = new HashSet<>(acceptedMediaTypes.size());
	for (Preference<MediaType> p : acceptedMediaTypes) {
		accepted.add(p.getMetadata());
	}
	
	accepted.removeAll(Arrays.asList(types));
	
	if (accepted.isEmpty()) {
		throw OntopiaRestErrors.UNSUPPORTED_MIME_TYPE.build(getClass().getName(), Arrays.toString(types));
	}
}
 
Example #3
Source File: ExtensionMediaTypeFilter.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
protected int beforeHandle( Request request, Response response )
{
    List<String> segments = request.getResourceRef().getSegments();
    if( segments.get( segments.size() - 1 ).equals( "" ) )
    {
        return Filter.CONTINUE;
    }

    String extensions = request.getResourceRef().getExtensions();
    if( extensions != null )
    {
        int idx = extensions.lastIndexOf( "." );
        if( idx != -1 )
        {
            extensions = extensions.substring( idx + 1 );
        }

        MetadataService metadataService = getApplication().getMetadataService();
        Metadata metadata = metadataService.getMetadata( extensions );
        if( metadata != null && metadata instanceof MediaType )
        {
            request.getClientInfo()
                .setAcceptedMediaTypes( Collections.singletonList( new Preference<MediaType>( (MediaType) metadata ) ) );
            String path = request.getResourceRef().getPath();
            path = path.substring( 0, path.length() - extensions.length() - 1 );
            request.getResourceRef().setPath( path );
        }
    }

    return Filter.CONTINUE;
}
 
Example #4
Source File: ExtensionMediaTypeFilter.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
protected int beforeHandle( Request request, Response response )
{
    List<String> segments = request.getResourceRef().getSegments();
    if (segments.get( segments.size()-1 ).equals(""))
      return Filter.CONTINUE;

    String extensions = request.getResourceRef().getExtensions();
    if( extensions != null )
    {
        int idx = extensions.lastIndexOf( "." );
        if( idx != -1 )
        {
            extensions = extensions.substring( idx + 1 );
        }

        MetadataService metadataService = getApplication().getMetadataService();
        Metadata metadata = metadataService.getMetadata( extensions );
        if( metadata != null && metadata instanceof MediaType )
        {
            request.getClientInfo()
                .setAcceptedMediaTypes( Collections.singletonList( new Preference<>( (MediaType) metadata ) ) );
            String path = request.getResourceRef().getPath();
            path = path.substring( 0, path.length() - extensions.length() - 1 );
            request.getResourceRef().setPath( path );
        }
    }

    return Filter.CONTINUE;
}
 
Example #5
Source File: ContextResourceClientFactory.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
public void setAcceptedLanguages(Language... acceptedLanguages)
{
    List<Preference<Language>> languages = new ArrayList<>();
    for( Language acceptedLanguage : acceptedLanguages )
    {
        languages.add( new Preference<>());
    }

    info.setAcceptedLanguages( languages );

}
 
Example #6
Source File: ContextResourceClientFactory.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
public void setAcceptedMediaTypes(MediaType... acceptedMediaTypes)
{
    List<Preference<MediaType>> mediatypes = new ArrayList<>();
    for( MediaType mediaType : acceptedMediaTypes )
    {
        mediatypes.add( new Preference<>( mediaType ) );
    }

    info.setAcceptedMediaTypes( mediatypes );
}
 
Example #7
Source File: PolygeneConverter.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public <T> void updatePreferences( List<Preference<MediaType>> preferences,
                                   Class<T> entity
)
{
    updatePreferences( preferences, MediaType.APPLICATION_JSON, 1.0F );
}
 
Example #8
Source File: AbstractOntopiaResource.java    From ontopia with Apache License 2.0 5 votes vote down vote up
@Override
protected void doError(Status status) {
	if (status.equals(Status.CLIENT_ERROR_NOT_ACCEPTABLE) && (status.getThrowable() == null)) {
		// we have an ontopia error for this
		List<Preference<MediaType>> acceptedMediaTypes = getClientInfo().getAcceptedMediaTypes();
		List<MediaType> accepted = new ArrayList<>(acceptedMediaTypes.size());
		for (Preference<MediaType> mt : acceptedMediaTypes) {
			accepted.add(mt.getMetadata());
		}
		
		super.doError(OntopiaRestErrors.UNSUPPORTED_MIME_TYPE.build(getClass().getName(), Arrays.toString(accepted.toArray())).getStatus());
	} else {
		super.doError(status);
	}
}