@Override public String toString() { return (getRequest() == null ? "" : getRequest().toString()) + (getResponse() == null ? "" : " => " + getResponse().toString()); } }
/** * Invoked when a {@link Throwable} is caught during initialization, * handling or releasing. * * @param throwable * The caught error or exception. */ protected void doCatch(Throwable throwable) { getLogger().log(Level.INFO, "Exception or error caught in resource", throwable); }
/** * Returns the preferred representation according to the client preferences * specified in the request. By default it calls the * {@link #represent(Variant)} method with the preferred variant returned by * {@link #getPreferredVariant()}. * * @return The preferred representation. * @see #getPreferredVariant() * @throws ResourceException */ public Representation represent() throws ResourceException { return represent(getPreferredVariant()); }
/** * Returns the request associated to this exception. * * @return The request associated to this exception. */ public Request getRequest() { return (this.resource != null) ? this.resource.getRequest() : null; }
/** * Returns the response associated to this exception. * * @return The response associated to this exception. */ public Response getResponse() { return (this.resource != null) ? this.resource.getResponse() : null; }
if (!isAvailable()) { getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); } else { final List<Variant> variants = getVariants(); if ((variants == null) || (variants.isEmpty())) { getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); getLogger() .warning( "A resource should normally have at least one variant added by calling getVariants().add() in the constructor. Check your resource \"" + getRequest().getResourceRef() + "\"."); } else if (isNegotiateContent()) { final Variant preferredVariant = getPreferredVariant(); getResponse().setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE); getResponse().setEntity(refs.getTextRepresentation()); } else { getResponse().getDimensions().clear(); getResponse().getDimensions().add(Dimension.CHARACTER_SET); getResponse().getDimensions().add(Dimension.ENCODING); getResponse().getDimensions().add(Dimension.LANGUAGE); getResponse().getDimensions().add(Dimension.MEDIA_TYPE); getResponse() .setEntity(getRepresentation(preferredVariant));
if (getRequest().getConditions().hasSome()) { Variant preferredVariant = null; if (isNegotiateContent()) { preferredVariant = getPreferredVariant(); } else { final List<Variant> variants = getVariants(); getResponse().setStatus( Status.CLIENT_ERROR_PRECONDITION_FAILED); canPut = false; final Status status = getRequest().getConditions().getStatus( getRequest().getMethod(), getRepresentation(preferredVariant)); if (status != null) { getResponse().setStatus(status); canPut = false; final Object oHeaders = getRequest().getAttributes().get( "org.restlet.http.headers"); if (oHeaders != null) { final Series<Parameter> headers = (Series<Parameter>) oHeaders; if (headers.getFirst("Content-Range", true) != null) { getResponse() .setStatus( new Status( put(getRequest().getEntity());
public void handleDelete() { boolean canDelete = true; if (getRequest().getConditions().hasSome()) { Variant preferredVariant = null; if (isNegotiateContent()) { preferredVariant = getPreferredVariant(); } else { final List<Variant> variants = getVariants(); getResponse().setStatus( Status.CLIENT_ERROR_PRECONDITION_FAILED); canDelete = false; final Status status = getRequest().getConditions().getStatus( getRequest().getMethod(), getRepresentation(preferredVariant)); getResponse().setStatus(status); canDelete = false; delete();
/** * Handles a POST call by invoking the * {@link #acceptRepresentation(Representation)} method. It also logs a * trace if there is no entity posted. */ @Override public void handlePost() { if (!getRequest().isEntityAvailable()) { getLogger() .fine( "POST request received without any entity. Continuing processing."); } post(getRequest().getEntity()); }
Resource resource) throws IOException { Representation result = null; boolean loggable = (resource == null) ? true : resource.isLoggable(); ConverterHelper ch = ConverterUtils.getBestHelper(source, target, resource); target = resource.getConnegService() .getPreferredVariant(variants, resource.getRequest(), resource.getMetadataService()); } else { target = variants.get(0); result.setMediaType(target.getMediaType()); } else if (resource != null) { result.setMediaType(resource.getMetadataService() .getDefaultMediaType()); } else {
/** * Returns the preferred representation according to the client preferences * specified in the request. * * @return The preferred representation. * @deprecated Use the {@link #represent()} method instead. * @see #getPreferredVariant() */ @Deprecated public Representation getPreferredRepresentation() { Representation result = null; try { result = represent(); } catch (ResourceException re) { getResponse().setStatus(re.getStatus(), re); } return result; }
/** * Sets the query value for the named parameter. If no query is defined, it * creates one. If the same parameter exists, it replaces it altogether. * * @param name * The query parameter name. * @param value * The query parameter value. */ public void setQueryValue(String name, String value) { Form query = getQuery(); if (query == null) { query = new Form(); } query.set(name, value); try { getReference().setQuery(query.encode()); } catch (IOException e) { getLogger().fine("Unable to set the query value"); } }
/** * Asks the resource to delete itself and all its representations.The * default behavior is to invoke the {@link #removeRepresentations()} * method. * * @deprecated Use the {@link #removeRepresentations()} method instead. */ @Deprecated public void delete() { try { removeRepresentations(); } catch (ResourceException re) { getResponse().setStatus(re.getStatus(), re); } }
/** * Posts a representation to the resource. The default behavior is to invoke * the {@link #acceptRepresentation(Representation)} method. * * @param entity * The representation posted. * @deprecated Use the {@link #acceptRepresentation(Representation)} method * instead. */ @Deprecated public void post(Representation entity) { try { acceptRepresentation(entity); } catch (ResourceException re) { getResponse().setStatus(re.getStatus(), re); } }
/** * Puts a representation in the resource. The default behavior is to invoke * the {@link #storeRepresentation(Representation)} method. * * @param entity * The representation put. * @deprecated Use the {@link #storeRepresentation(Representation)} method * instead. */ @Deprecated public void put(Representation entity) { try { storeRepresentation(entity); } catch (ResourceException re) { getResponse().setStatus(re.getStatus(), re); } }
/** * Returns the preferred variant according to the client preferences * specified in the request. * * @return The preferred variant. */ public Variant getPreferredVariant() { Variant result = null; final List<Variant> variants = getVariants(); if ((variants != null) && (!variants.isEmpty())) { Language language = null; // Compute the preferred variant. Get the default language // preference from the Application (if any). final Application app = Application.getCurrent(); if (app != null) { language = app.getMetadataService().getDefaultLanguage(); } result = getRequest().getClientInfo().getPreferredVariant(variants, language); } return result; }
/** * Returns the resource reference's optional matrix. * * @return The resource reference's optional matrix. * @see Reference#getMatrixAsForm() */ public Form getMatrix() { return getReference() == null ? null : getReference().getMatrixAsForm(); }
public Resource findTarget(Request request, Response response){ Resource res = (Resource) applicationContext.getBean(getBeanToFind()); res.init(getContext(), request, response); return res; } }
/** * Returns the first value of the query parameter given its name if * existing, or null. * * @param name * The query parameter name. * @return The first value of the query parameter. */ public static String getQueryValue(String name) { String result = null; Map<String, ArrayList<String>> query = getQuery(); if (query != null) { result = query.get(name).get(0); } return result; }
/** * 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(); }