Example #1
0
  /**
   * Handles a call for a local entity. By default, only GET and HEAD methods are implemented.
   *
   * @param request The request to handle.
   * @param response The response to update.
   * @param decodedPath The URL decoded entity path.
   */
  @Override
  protected void handleLocal(Request request, Response response, String decodedPath) {
    int spi = decodedPath.indexOf("!/");
    String fileUri;
    String entryName;
    if (spi != -1) {
      fileUri = decodedPath.substring(0, spi);
      entryName = decodedPath.substring(spi + 2);
    } else {
      fileUri = decodedPath;
      entryName = "";
    }

    LocalReference fileRef = new LocalReference(fileUri);
    if (Protocol.FILE.equals(fileRef.getSchemeProtocol())) {
      final File file = fileRef.getFile();
      if (Method.GET.equals(request.getMethod()) || Method.HEAD.equals(request.getMethod())) {
        handleGet(request, response, file, entryName, getMetadataService());
      } else if (Method.PUT.equals(request.getMethod())) {
        handlePut(request, response, file, entryName);
      } else {
        response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
        response.getAllowedMethods().add(Method.GET);
        response.getAllowedMethods().add(Method.HEAD);
        response.getAllowedMethods().add(Method.PUT);
      }
    } else {
      response.setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED, "Only works on local files.");
    }
  }
Example #2
0
 public void doPut(String path, String fileToPut) {
   ClientResource cr = new ClientResource(testContext, BASE_URI + path);
   LocalReference ref = new LocalReference(fileToPut);
   ref.setProtocol(Protocol.CLAP);
   ClientResource local = new ClientResource(ref);
   Representation rep = local.get();
   if (fileToPut.endsWith(".csv")) rep.setMediaType(MediaType.TEXT_CSV);
   try {
     cr.put(rep);
   } catch (ResourceException e) {
     e.printStackTrace();
     Assert.fail();
   }
 }
  public void testRepresentationTemplate() throws Exception {
    // Create a temporary directory for the tests
    File testDir = new File(System.getProperty("java.io.tmpdir"), "VelocityTestCase");
    testDir.mkdir();

    // Create a temporary template file
    File testFile = File.createTempFile("test", ".vm", testDir);
    FileWriter fw = new FileWriter(testFile);
    fw.write("Value=$value");
    fw.close();

    Map<String, Object> map = new TreeMap<String, Object>();
    map.put("value", "myValue");

    // Representation approach
    Reference ref = LocalReference.createFileReference(testFile);
    ClientResource r = new ClientResource(ref);
    Representation templateFile = r.get();
    TemplateRepresentation tr = new TemplateRepresentation(templateFile, map, MediaType.TEXT_PLAIN);
    final String result = tr.getText();
    assertEquals("Value=myValue", result);

    // Clean-up
    BioUtils.delete(testFile);
    BioUtils.delete(testDir, true);
  }
 @Override
 public Restlet createRoot() {
   // Create a Directory that manages a local directory
   this.directory =
       new Directory(getContext(), LocalReference.createFileReference(getTestDirectory()));
   this.directory.setNegotiateContent(true);
   return new org.restlet.ext.freemarker.TemplateFilter(getContext(), this.directory);
 }
Example #5
0
  /**
   * Handles a GET call.
   *
   * @param request The request to answer.
   * @param response The response to update.
   * @param file The Zip archive file.
   * @param entryName The Zip archive entry name.
   * @param metadataService The metadata service.
   */
  protected void handleGet(
      Request request,
      Response response,
      File file,
      String entryName,
      final MetadataService metadataService) {

    if (!file.exists()) {
      response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    } else {
      ZipFile zipFile;

      try {
        zipFile = new ZipFile(file);
      } catch (Exception e) {
        response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
        return;
      }

      Entity entity = new ZipEntryEntity(zipFile, entryName, metadataService);
      if (!entity.exists()) {
        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
      } else {
        final Representation output;

        if (entity.isDirectory()) {
          // Return the directory listing
          final Collection<Entity> children = entity.getChildren();
          final ReferenceList rl = new ReferenceList(children.size());
          String fileUri = LocalReference.createFileReference(file).toString();
          String scheme = request.getResourceRef().getScheme();
          String baseUri = scheme + ":" + fileUri + "!/";

          for (final Entity entry : children) {
            rl.add(baseUri + entry.getName());
          }

          output = rl.getTextRepresentation();

          try {
            zipFile.close();
          } catch (IOException e) {
            // Do something ???
          }
        } else {
          // Return the file content
          output = entity.getRepresentation(metadataService.getDefaultMediaType(), getTimeToLive());
          output.setLocationRef(request.getResourceRef());
          Entity.updateMetadata(entity.getName(), output, true, getMetadataService());
        }

        response.setStatus(Status.SUCCESS_OK);
        response.setEntity(output);
      }
    }
  }
 @Override
 public Restlet createInboundRoot() {
   Router router = new Router();
   router.attach("/test", new TestRangeRestlet());
   router.attach("/testGet", new TestRangeGetRestlet());
   Directory directory =
       new Directory(getContext(), LocalReference.createFileReference(testDir));
   directory.setModifiable(true);
   router.attach("/testPut/", directory);
   return router;
 }
  @Override
  public void challenge(Response response, boolean stale) {
    // Load the FreeMarker template
    Representation ftl =
        new ClientResource(
                LocalReference.createClapReference(getClass().getPackage()) + "/Login.ftl")
            .get();

    // Wraps the bean with a FreeMarker representation
    response.setEntity(
        new TemplateRepresentation(
            ftl, response.getRequest().getResourceRef(), MediaType.TEXT_HTML));
    response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
  }
Example #8
0
 @Override
 public Representation find(String path) {
   path = path.replace(getServerUrl(), "");
   if (!UrlHelper.hasExtension(path)) path = path + ".xml";
   LocalReference ref = LocalReference.createClapReference(path);
   try {
     return new ClientResource(ref).get();
   } catch (ResourceException e) {
     File file = new File("src/main/webapp" + path);
     if (file.exists() && !file.isDirectory())
       return new FileRepresentation(file, MediaType.TEXT_PLAIN);
     throw new ResourceException(404);
   }
 }
  @Get
  @Override
  public Representation get() {

    getApplication().getLogger().info("get adminIndex");

    AdminIndexDTO aid = new AdminIndexDTO();

    // Dynamic sitools url
    aid.setAppUrl(application.getSettings().getString(Consts.APP_URL));

    Reference ref = LocalReference.createFileReference(application.getAdminIndexUrl());

    Representation adminFtl = new ClientResource(ref).get();

    // Wraps the bean with a FreeMarker representation
    return new TemplateRepresentation(adminFtl, aid, MediaType.TEXT_HTML);
  }
  @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;
  }