/** * 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(); }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { return equals(obj, false); }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { return equals(obj, false); }
/** {@inheritDoc} */ @Override public boolean equals(Object obj) { return equals(obj, false); }
/** * Returns the media type mapped to an extension, or null if no such mapping exists. * * @param mediaType the media type. * * @return THe extension, or null. * @see {@link #registerExtension(String, MediaType)} */ public static String getExtensionForMediaType( MediaType mediaType ) { for ( Map.Entry<String, MediaType> e : instance.extensions.entrySet() ) { if ( e.getValue().equals( mediaType ) ) { return e.getKey(); } } return null; }
private boolean isMediaType(Representation entity, MediaType desiredType) { if (entity == null) { return false; } return desiredType.equals(entity.getMediaType(), true); }
public static SparqlResultFormat lookup(final MediaType mediaType) { for (SparqlResultFormat f : SparqlResultFormat.values()) { if (f.mediaType.equals(mediaType)) { return f; } } return null; }
protected boolean isXml(MediaType type) { String isxml = type.getSubType(); return MediaType.APPLICATION_XML.equals(type) || isxml.endsWith("+xml"); }
protected boolean isXml(MediaType type) { String isxml = type.getSubType(); return MediaType.APPLICATION_XML.equals(type) || isxml.endsWith("+xml"); }
public static SparqlResultFormat lookup(final MediaType mediaType) { for (SparqlResultFormat f : SparqlResultFormat.values()) { if (f.mediaType.equals(mediaType)) { return f; } } return null; }
@Override public long getSize() { long result = wrappedRepresentation.getSize(); if (result > 0 && MediaType.APPLICATION_JSON.equals(wrappedRepresentation .getMediaType())) { try { java.io.StringWriter sw = new java.io.StringWriter(); write(sw); sw.flush(); return sw.toString().length(); } catch (IOException e) { return UNKNOWN_SIZE; } } return UNKNOWN_SIZE; }
@Override protected Representation get( Variant variant ) throws ResourceException { if ( MediaType.APPLICATION_JSON.equals( variant.getMediaType() ) ) { LOGGER.trace( "Will represent {}", MediaType.APPLICATION_JSON ); return representJson(); } else if ( MediaType.TEXT_HTML.equals( variant.getMediaType() ) ) { LOGGER.trace( "Will represent {}", MediaType.TEXT_HTML ); return representHtml(); } throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND ); }
@Override public long getSize() { long result = wrappedRepresentation.getSize(); if (result > 0 && MediaType.APPLICATION_JSON.equals(wrappedRepresentation .getMediaType())) { try { java.io.StringWriter sw = new java.io.StringWriter(); write(sw); sw.flush(); return sw.toString().length(); } catch (IOException e) { return UNKNOWN_SIZE; } } return UNKNOWN_SIZE; }
/** * Constructor. * * @param formRepresentation * The representation to parse. */ public FormDataSet(Representation formRepresentation) { this(); if ((formRepresentation != null) && MediaType.APPLICATION_WWW_FORM.equals(formRepresentation .getMediaType())) { FormUtils.parse(this.entries, formRepresentation); } }
@Override public Representation get( Variant variant ) throws ResourceException { if( variant.getMediaType().equals( MediaType.APPLICATION_RDF_XML ) ) { return new RdfXmlOutputRepresentation(); } else if( variant.getMediaType().equals( MediaType.APPLICATION_RDF_TRIG ) ) { return new RdfTrigOutputRepresentation( MediaType.APPLICATION_RDF_TRIG ); } else if( variant.getMediaType().equals( MediaType.TEXT_PLAIN ) ) { return new RdfTrigOutputRepresentation( MediaType.TEXT_PLAIN ); } return null; }
/** * Returns an actual index representation for a given variant. * * @param variant * The selected variant. * @param indexContent * The directory index to represent. * @return The actual index representation. */ public Representation getIndexRepresentation(Variant variant, ReferenceList indexContent) { Representation result = null; if (variant.getMediaType().equals(MediaType.TEXT_HTML)) { result = indexContent.getWebRepresentation(); } else if (variant.getMediaType().equals(MediaType.TEXT_URI_LIST)) { result = indexContent.getTextRepresentation(); } return result; }
private static OrderedMap<ResourceMethod, List<MediaType>> findMethodsSupportAllTypes( Collection<ResourceMethod> resourceMethods, ConsOrProdMime inOut) { final OrderedMap<ResourceMethod, List<MediaType>> returnMethods = new OrderedMap<>(); for (final ResourceMethod resourceMethod : resourceMethods) { final List<MediaType> mimes = getConsOrProdMimes(resourceMethod, inOut); for (final MediaType resMethMediaType : mimes) { if (resMethMediaType.equals(MediaType.ALL)) { returnMethods.put(resourceMethod, mimes); } } } return returnMethods; }
protected Representation serialize(Context context, Request req, Variant variant, Object payload) throws IOException { // TEXT_HTML is requested by direct browsing (IE) // APPLICATION_XML is requested by direct browsing (FF) if (MediaType.TEXT_HTML.equals(variant.getMediaType())) { HashMap<String, Object> dataModel = new HashMap<String, Object>(); dataModel.put("listItems", sortContentListResource(((ContentListResourceResponse) payload).getData())); dataModel.put("request", req); dataModel.put("nexusVersion", applicationStatusSource.getSystemStatus().getVersion()); dataModel.put("nexusRoot", BaseUrlHolder.get()); final VelocityRepresentation representation = new VelocityRepresentation(context, "/templates/repositoryContentHtml.vm", getClass().getClassLoader(), dataModel, variant.getMediaType()); return representation; } return null; }
@Override public void doError(Status errorStatus) { Representation representation = getResponse().getEntity(); if (representation.getMediaType().equals(MediaType.APPLICATION_JSON)) { // Do not throw an exception here. getLogger().fine("OAuth response is found."); // XXX: after #doError, the representation will disposed in // #handleInbound. return; } // ResourceException will be thrown. super.doError(errorStatus); }
@Override public boolean writeResponse( final Object result, final Response response ) throws ResourceException { MediaType type = getVariant( response.getRequest(), ENGLISH, supportedMediaTypes ).getMediaType(); if( MediaType.APPLICATION_JSON.equals( type ) ) { if( result instanceof String || result instanceof Number || result instanceof Boolean ) { StringRepresentation representation = new StringRepresentation( result.toString(), MediaType.APPLICATION_JSON ); response.setEntity( representation ); return true; } } return false; } }