org.restlet.data.Language Java Examples

The following examples show how to use org.restlet.data.Language. 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: Variant.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * Indicates if the current variant includes the given variant.
 * 
 * @param other
 *            The other variant.
 * @return True if the current variant includes the other.
 */
public boolean includes(Variant other) {
    boolean result = other != null;

    // Compare the character set
    if (result) {
        result = (getCharacterSet() == null)
                || getCharacterSet().includes(other.getCharacterSet());
    }

    // Compare the media type
    if (result) {
        result = (getMediaType() == null)
                || getMediaType().includes(other.getMediaType());
    }

    // Compare the languages
    if (result) {
        result = (getLanguages().isEmpty())
                || getLanguages().contains(Language.ALL)
                || getLanguages().containsAll(other.getLanguages());
    }

    return result;
}
 
Example #3
Source File: AbstractResponseWriter.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
protected Variant getVariant( Request request,
                              List<Language> possibleLanguages,
                              List<MediaType> possibleMediaTypes
)
{
    Language language = request.getClientInfo().getPreferredLanguage( possibleLanguages );

    if( language == null )
    {
        language = possibleLanguages.get( 0 );
    }

    MediaType responseType = request.getClientInfo().getPreferredMediaType( possibleMediaTypes );

    if( responseType == null && request.getClientInfo()
                                    .getPreferredMediaType( Collections.singletonList( MediaType.ALL ) ) == MediaType.ALL )
    {
        responseType = possibleMediaTypes.get( 0 );
    }

    Variant variant = new Variant( responseType, language );
    variant.setCharacterSet( CharacterSet.UTF_8 );

    return variant;
}
 
Example #4
Source File: Variant.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param mediaType
 *            The media type.
 * @param language
 *            The language.
 */
public Variant(MediaType mediaType, Language language) {
    this.characterSet = null;

    if (language != null) {
        getLanguages().add(language);
    } else {
        this.languages = null;
    }

    this.mediaType = mediaType;
    this.locationRef = null;
}
 
Example #5
Source File: EntityResource.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private Representation entityHeaders( Representation representation, EntityState entityState )
{
    representation.setModificationDate( java.util.Date.from( entityState.lastModified() ) );
    representation.setTag( new Tag( "" + entityState.version() ) );
    representation.setCharacterSet( CharacterSet.UTF_8 );
    representation.setLanguages( Collections.singletonList( Language.ENGLISH ) );

    return representation;
}
 
Example #6
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 #7
Source File: Variant.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * Returns the modifiable list of languages. Creates a new instance if no
 * one has been set. An "IllegalArgumentException" exception is thrown when
 * adding a null language to this list.<br>
 * <br>
 * Note that when used with HTTP connectors, this property maps to the
 * "Content-Language" header.
 * 
 * @return The list of languages.
 */
public List<Language> getLanguages() {
    if (this.languages == null) {
        this.languages = new WrapperList<Language>() {

            @Override
            public void add(int index, Language element) {
                if (element == null) {
                    throw new IllegalArgumentException(
                            "Cannot add a null language.");
                }

                super.add(index, element);
            }

            @Override
            public boolean add(Language element) {
                if (element == null) {
                    throw new IllegalArgumentException(
                            "Cannot add a null language.");
                }

                return super.add(element);
            }

            @Override
            public boolean addAll(Collection<? extends Language> elements) {
                boolean addNull = (elements == null);
                if (!addNull) {
                    for (final Iterator<? extends Language> iterator = elements
                            .iterator(); !addNull && iterator.hasNext();) {
                        addNull = (iterator.next() == null);
                    }
                }
                if (addNull) {
                    throw new IllegalArgumentException(
                            "Cannot add a null language.");
                }

                return super.addAll(elements);
            }

            @Override
            public boolean addAll(int index,
                    Collection<? extends Language> elements) {
                boolean addNull = (elements == null);
                if (!addNull) {
                    for (final Iterator<? extends Language> iterator = elements
                            .iterator(); !addNull && iterator.hasNext();) {
                        addNull = (iterator.next() == null);
                    }
                }
                if (addNull) {
                    throw new IllegalArgumentException(
                            "Cannot add a null language.");
                }

                return super.addAll(index, elements);
            }

        };
    }
    return this.languages;
}
 
Example #8
Source File: StringRepresentation.java    From DeviceConnect-Android with MIT License 3 votes vote down vote up
/**
 * Constructor.
 * 
 * @param text
 *            The string value.
 * @param mediaType
 *            The media type.
 * @param language
 *            The language.
 * @param characterSet
 *            The character set.
 */
public StringRepresentation(CharSequence text, MediaType mediaType,
        Language language, CharacterSet characterSet) {
    super(mediaType);
    setMediaType(mediaType);
    if (language != null) {
        getLanguages().add(language);
    }

    setCharacterSet(characterSet);
    setText(text);
}
 
Example #9
Source File: Variant.java    From DeviceConnect-Android with MIT License 2 votes vote down vote up
/**
 * Sets the list of languages.<br>
 * <br>
 * Note that when used with HTTP connectors, this property maps to the
 * "Content-Language" header.
 * 
 * @param languages
 *            The list of languages.
 */
public void setLanguages(List<Language> languages) {
    this.languages = languages;
}
 
Example #10
Source File: StringRepresentation.java    From DeviceConnect-Android with MIT License 2 votes vote down vote up
/**
 * Constructor. The following metadata are used by default: "text/plain"
 * media type, no language and the UTF-8 character set.
 * 
 * @param text
 *            The string value.
 * @param language
 *            The language.
 */
public StringRepresentation(CharSequence text, Language language) {
    this(text, MediaType.TEXT_PLAIN, language);
}
 
Example #11
Source File: StringRepresentation.java    From DeviceConnect-Android with MIT License 2 votes vote down vote up
/**
 * Constructor. The following metadata are used by default: UTF-8 character
 * set.
 * 
 * @param text
 *            The string value.
 * @param mediaType
 *            The media type.
 * @param language
 *            The language.
 */
public StringRepresentation(CharSequence text, MediaType mediaType,
        Language language) {
    this(text, mediaType, language, CharacterSet.UTF_8);
}