/** * If client can accept text/html, always prefer it. WebKit-based browsers * claim to want application/xml, but we don't want to give it to them. See * <a href="https://webarchive.jira.com/browse/HER-1603">https://webarchive.jira.com/browse/HER-1603</a> */ public Variant getPreferredVariant() { boolean addExplicitTextHtmlPreference = false; for (Preference<MediaType> mediaTypePreference: getRequest().getClientInfo().getAcceptedMediaTypes()) { if (mediaTypePreference.getMetadata().equals(MediaType.TEXT_HTML)) { mediaTypePreference.setQuality(Float.MAX_VALUE); addExplicitTextHtmlPreference = false; break; } else if (mediaTypePreference.getMetadata().includes(MediaType.TEXT_HTML)) { addExplicitTextHtmlPreference = true; } } if (addExplicitTextHtmlPreference) { List<Preference<MediaType>> acceptedMediaTypes = getRequest().getClientInfo().getAcceptedMediaTypes(); acceptedMediaTypes.add(new Preference<MediaType>(MediaType.TEXT_HTML, Float.MAX_VALUE)); getRequest().getClientInfo().setAcceptedMediaTypes(acceptedMediaTypes); } return super.getPreferredVariant(); }
/** * Sets the media type preferences. Note that when used with HTTP * connectors, this property maps to the "Accept" header. * * @param acceptedMediaTypes * The media type preferences. */ public void setAcceptedMediaTypes( List<Preference<MediaType>> acceptedMediaTypes) { synchronized (this) { List<Preference<MediaType>> ac = getAcceptedMediaTypes(); ac.clear(); ac.addAll(acceptedMediaTypes); } }
/** * Returns the preferred media type among a list of supported ones, based on * the client preferences. * * @param supported * The supported media types. * @return The preferred media type. */ public MediaType getPreferredMediaType(List<MediaType> supported) { return getPreferredMetadata(supported, getAcceptedMediaTypes()); }
/** * Constructor from a media type. * * @param mediaType * The preferred media type. */ public ClientInfo(MediaType mediaType) { getAcceptedMediaTypes().add(new Preference<MediaType>(mediaType)); }
/** * 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)); } }
/** * Parses media type preferences from a header. * * @param acceptMediaTypeHeader * The header to parse. * @param clientInfo * The client info to update. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void addMediaTypes(String acceptMediaTypeHeader, ClientInfo clientInfo) { if (acceptMediaTypeHeader != null) { PreferenceReader pr = new PreferenceReader( PreferenceReader.TYPE_MEDIA_TYPE, acceptMediaTypeHeader); pr.addValues(clientInfo.getAcceptedMediaTypes()); } else { clientInfo.getAcceptedMediaTypes().add( new Preference(MediaType.ALL)); } }
/** * Returns the enriched list of media type preferences. * * @return The enriched list of media type preferences. */ protected List<Preference<MediaType>> getMediaTypePrefs() { return getRequest().getClientInfo().getAcceptedMediaTypes(); }
/** * Constructor from a list of variants. Note that only media types are taken * into account. * * @param variants * The variants corresponding to the accepted media types. */ public ClientInfo( List<? extends org.restlet.representation.Variant> variants) { if (variants != null) { for (org.restlet.representation.Variant variant : variants) { getAcceptedMediaTypes().add( new Preference<MediaType>(variant.getMediaType())); } } }
@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); } }
/** * A beforeHandle will simply embed in request attributes a Nexus interface implemntor, depending on key used to * name it. */ protected int beforeHandle( Request request, Response response ) { String agentInfo = request.getClientInfo().getAgent() != null ? request .getClientInfo().getAgent().toLowerCase() : "unknown"; // This solution was the only that came on my mind :) // should work only if client specified more then one "alternatives" if ( StringUtils.indexOfAny( agentInfo, new String[] { "mozilla", "firefox", "msie", "opera", "safari" } ) > -1 && request.getClientInfo().getAcceptedMediaTypes().size() > 1 ) { // overriding client preferences, since it is a browser to TEXT/HTML // doing this by adding text/html as firxt to accepted media types with quality 1 request .getClientInfo().getAcceptedMediaTypes().add( 0, new Preference<MediaType>( MediaType.TEXT_HTML, 1 ) ); } return CONTINUE; }
/** * Updates the client info with the given metadata. It clears existing * preferences for the same type of metadata if necessary. * * @param clientInfo * The client info to update. * @param metadata * The metadata to use. */ private void updateMetadata(ClientInfo clientInfo, Metadata metadata) { if (metadata != null) { if (metadata instanceof CharacterSet) { clientInfo.getAcceptedCharacterSets().clear(); clientInfo.getAcceptedCharacterSets().add( new Preference<CharacterSet>((CharacterSet) metadata)); } else if (metadata instanceof Encoding) { clientInfo.getAcceptedEncodings().clear(); clientInfo.getAcceptedEncodings().add( new Preference<Encoding>((Encoding) metadata)); } else if (metadata instanceof Language) { clientInfo.getAcceptedLanguages().clear(); clientInfo.getAcceptedLanguages().add( new Preference<Language>((Language) metadata)); } else if (metadata instanceof MediaType) { clientInfo.getAcceptedMediaTypes().clear(); clientInfo.getAcceptedMediaTypes().add( new Preference<MediaType>((MediaType) metadata)); } } }
/** * Updates the client preferences to accept the given metadata (media types, * character sets, etc.) with a given quality in addition to existing ones. * * @param metadata * The metadata to accept. * @param quality * The quality to set. */ public void accept(Metadata metadata, float quality) { if (metadata instanceof MediaType) { getAcceptedMediaTypes().add( new Preference<MediaType>((MediaType) metadata, quality)); } else if (metadata instanceof Language) { getAcceptedLanguages().add( new Preference<Language>((Language) metadata, quality)); } else if (metadata instanceof Encoding) { getAcceptedEncodings().add( new Preference<Encoding>((Encoding) metadata, quality)); } else { getAcceptedCharacterSets().add( new Preference<CharacterSet>((CharacterSet) metadata, quality)); } }
private Request makeRequest(Reference uri, MediaType accepts) { Request request = new Request(); logger.debug("Making new request for " + uri + " -- " + uri.getTargetRef()); request.setResourceRef(uri.getTargetRef()); if (accepts != null) { request.getClientInfo().getAcceptedMediaTypes().add( new Preference<MediaType>(accepts)); } if (baseURI.isParent(uri) && username != null) { logger.debug("Authenticating as " + username); ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password); request.setChallengeResponse(challengeResponse); } else { logger.warn("Not supplying credentials for out-of-site URI " + uri); } return request; }
request.getClientInfo().getAcceptedMediaTypes().add( new Preference<MediaType>( representation.getMediaType() ) );
request.getClientInfo().getAcceptedMediaTypes().add( new Preference<MediaType>(representation.getMediaType()));
/** * If client can accept text/html, always prefer it. WebKit-based browsers * claim to want application/xml, but we don't want to give it to them. See * <a href="https://webarchive.jira.com/browse/HER-1603">https://webarchive.jira.com/browse/HER-1603</a> */ public Variant getPreferredVariant() { boolean addExplicitTextHtmlPreference = false; for (Preference<MediaType> mediaTypePreference: getRequest().getClientInfo().getAcceptedMediaTypes()) { if (mediaTypePreference.getMetadata().equals(MediaType.TEXT_HTML)) { mediaTypePreference.setQuality(Float.MAX_VALUE); addExplicitTextHtmlPreference = false; break; } else if (mediaTypePreference.getMetadata().includes(MediaType.TEXT_HTML)) { addExplicitTextHtmlPreference = true; } } if (addExplicitTextHtmlPreference) { List<Preference<MediaType>> acceptedMediaTypes = getRequest().getClientInfo().getAcceptedMediaTypes(); acceptedMediaTypes.add(new Preference<MediaType>(MediaType.TEXT_HTML, Float.MAX_VALUE)); getRequest().getClientInfo().setAcceptedMediaTypes(acceptedMediaTypes); } return super.getPreferredVariant(); }
public Token requestToken(OAuthParameters parameters) throws OAuthException, IOException, JSONException { if (authenticationScheme == null) { // Use Body method setupBodyClientCredentials(parameters); } else { setChallengeResponse(authenticationScheme, clientId, clientSecret); } Representation input = parameters.toRepresentation(); getClientInfo().getAcceptedMediaTypes().add( new Preference<MediaType>(MediaType.APPLICATION_JSON)); JSONObject result = new JsonRepresentation(post(input)).getJsonObject(); if (result.has(ERROR)) { throw OAuthException.toOAuthException(result); } TokenResponse token = TokenResponse.parseResponse(result); if (token.scope == null) { // Should be identical to the scope requested by the client. token.scope = Scopes.parseScope(parameters.toForm().getFirstValue( SCOPE)); } return token; }
/** * Creates a {@link ClientInfo} instance with preferences matching exactly * the current variant. * * @return The new {@link ClientInfo} instance. */ public ClientInfo createClientInfo() { ClientInfo result = new ClientInfo(); if (getCharacterSet() != null) { result.getAcceptedCharacterSets().add( new Preference<CharacterSet>(getCharacterSet())); } if (getEncodings() != null) { for (Encoding encoding : getEncodings()) { result.getAcceptedEncodings().add( new Preference<Encoding>(encoding)); } } if (getLanguages() != null) { for (Language language : getLanguages()) { result.getAcceptedLanguages().add( new Preference<Language>(language)); } } if (getMediaType() != null) { result.getAcceptedMediaTypes().add( new Preference<MediaType>(getMediaType())); } return result; }
request.getClientInfo().getAcceptedMediaTypes(). add(new Preference<MediaType>(representation.getMediaType()));
.getDefaultLanguage(), Language.ALL); this.mediaTypePrefs = getEnrichedPreferences( clientInfo.getAcceptedMediaTypes(), (metadataService == null) ? null : metadataService .getDefaultMediaType(), MediaType.ALL);