/** * 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; }
@Override public Representation toRepresentation(Status status, Request request, Response response) { TemplateRepresentation representation = null; // Create the data model Map<String, Object> dataModel = new TreeMap<String, Object>(); dataModel.put("applicationName", Application.getCurrent().getName()); dataModel.put("statusReasonPhrase", response.getStatus().getReasonPhrase()); dataModel.put("statusDescription", response.getStatus().getDescription()); Representation thyFtl = new ClientResource( LocalReference.createClapReference(getClass().getPackage()) + "/RestStatus.vtl") .get(); try { representation = new TemplateRepresentation(thyFtl, dataModel, MediaType.TEXT_HTML); } catch (IOException e) { e.printStackTrace(); } return representation; }
/** * Returns a readable byte channel based on the given representation's content and its * write(WritableByteChannel) method. Internally, it uses a writer thread and a pipe channel. * * @param representation the representation to get the {@link OutputStream} from. * @return A readable byte channel. * @throws IOException */ public static ReadableByteChannel getChannel(final Representation representation) throws IOException { ReadableByteChannel result = null; if (Edition.CURRENT != Edition.GAE) { final java.nio.channels.Pipe pipe = java.nio.channels.Pipe.open(); org.restlet.Application application = org.restlet.Application.getCurrent(); // Get a thread that will handle the task of continuously // writing the representation into the input side of the pipe Runnable task = new Runnable() { public void run() { try { WritableByteChannel wbc = pipe.sink(); representation.write(wbc); wbc.close(); } catch (IOException ioe) { Context.getCurrentLogger() .log(Level.FINE, "Error while writing to the piped channel.", ioe); } } }; if (application != null && application.getTaskService() != null) { application.getTaskService().execute(task); } else { new Thread(task).start(); } result = pipe.source(); } else { Context.getCurrentLogger() .log( Level.WARNING, "The GAE edition is unable to return a channel for a representation given its write(WritableByteChannel) method."); } return result; }
/** * Constructor using the current application. * * @return A new application service * @see Application#getCurrent() */ public static ApplicationService create() { return create(Application.getCurrent()); }