Exemplo n.º 1
0
  public RESTComponent(Context context, Application[] applications) {
    super();
    this.getClients().add(Protocol.FILE);
    this.getClients().add(Protocol.HTTP);
    this.getClients().add(Protocol.HTTPS);

    for (Application application : applications) {
      application.setContext(context == null ? getContext().createChildContext() : context);
      getDefaultHost().attach(application);
    }
    getInternalRouter().attach("/", applications[0]);
  }
 @Before
 public void setUp() throws Exception {
   createProgrammes();
   TVProgrammeMetadataServer mockServer = mock(TVProgrammeMetadataServer.class);
   when(mockServer.getProgrammeStore()).thenReturn(store);
   Application.setCurrent(mockServer);
 }
Exemplo n.º 3
0
  @Override
  public synchronized void start() throws Exception {
    // restlet servlet engine will pass parameters from web.xml via the context parameters
    Series<Parameter> parameters = getContext().getParameters();
    dataSource = PSCPDataSource.createDataSource(parameters);
    daoFactory = PGDaoFactory.createPGDaoFactory(dataSource);
    lookupCache = new LookupCache(daoFactory);
    secretResolver = new SecretResolver();
    adminPage = parameters.getFirstValue("pscp.web.admin");
    urls = new URLS(adminPage);
    // links out to website
    urls.putStatic(
        new Template(getSlashedURL(parameters, "pscp.web.products")), URLS.Name.PRODUCT_LOCATION);
    urls.putStatic(
        new Template(getSlashedURL(parameters, "pscp.web.root")), URLS.Name.STATIC_MEDIA);

    // @todo how to deal with this?
    baseURIs = new ArrayList<String>(Arrays.asList("localhost:8080"));

    Map<String, Object> contextAttributes = getContext().getAttributes();
    contextAttributes.put(BaseResource.CONTEXT_ATTRIBUTE_DAO_FACTORY, daoFactory);
    contextAttributes.put(BaseResource.CONTEXT_ATTRIBUTE_URL_MAPPER, urls);
    contextAttributes.put(BaseResource.CONTEXT_ATTRIBUTE_LOOKUP_CACHE, lookupCache);
    contextAttributes.put(
        BaseResource.CONTEXT_ATTRIBUTE_PRODUCT_ROOT,
        getRootDir(BaseResource.CONTEXT_ATTRIBUTE_PRODUCT_ROOT, "pscp-products"));
    contextAttributes.put(
        BaseResource.CONTEXT_ATTRIBUTE_UPLOAD_ROOT,
        getRootDir(BaseResource.CONTEXT_ATTRIBUTE_UPLOAD_ROOT, "pscp-uploads"));

    super.start();
  }
Exemplo n.º 4
0
  /**
   * 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;
  }
Exemplo n.º 5
0
  /**
   * 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;
  }
  @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;
  }
 private String getMainNav(BundleContext bundleContext) {
   StringBuilder sb = new StringBuilder();
   List<Application> applications = ApplicationsService.getApplications(bundleContext);
   for (Application application : applications) {
     if (application.getName().equalsIgnoreCase("static")
         || application.getName().equalsIgnoreCase("default")) {
       continue;
     }
     String name =
         application.getName().substring(0, 1).toUpperCase() + application.getName().substring(1);
     sb.append("<li><a href='/")
         .append(application.getName())
         .append("'>")
         .append(name)
         .append("</a></li>\n");
   }
   return sb.toString();
 }
Exemplo n.º 8
0
 @Override
 public void setContext(Context context) {
   super.setContext(context);
 }
 @Override
 public void setContext(Context context) {
   super.setContext(context);
   this.jaxRsRestlet.setContext(context);
 }
Exemplo n.º 10
0
 /**
  * Constructor using the current application.
  *
  * @return A new application service
  * @see Application#getCurrent()
  */
 public static ApplicationService create() {
   return create(Application.getCurrent());
 }
Exemplo n.º 11
0
 /** Clears the thread local variables set by the Restlet API and engine. */
 public static void clearThreadLocalVariables() {
   Response.setCurrent(null);
   Context.setCurrent(null);
   org.restlet.routing.VirtualHost.setCurrent(null);
   org.restlet.Application.setCurrent(null);
 }