Ejemplo n.º 1
0
  public void initialise(String scriptFullName, ServiceOperationInterface parentService) {

    String currentDirectory;

    File file = new File(scriptFullName);
    currentDirectory = file.getParent();

    if (startDirectory != null) {
      /* Relative directory... */
      if (startDirectory.startsWith(".")) {
        startDirectory = currentDirectory + File.separator + startDirectory;
        file = new File(startDirectory);
        try {
          startDirectory = file.getCanonicalPath();
        } catch (Exception ex) {
          log.warn("Could not convert the path [" + file.getAbsolutePath() + "]: " + ex.toString());
        }
      }
      /* else absolute directory specified... therefore just use the specified directory... */
    } else {
      /* No start directory specified, so use the scripts' directory... */
      startDirectory = currentDirectory;
    }

    log.info("startDirectory => [" + startDirectory + "]");

    this.parentService = parentService;

    return;
  }
Ejemplo n.º 2
0
  public void invokeOperation(String responseFormat, ServiceScriptHelper helper) throws Exception {

    /* We expect that the properties file name is a parameter in the URL.. */

    String filename = parameters.getProperty(FILE_NAME_PARAM, "");

    if (filename.equals("")) {
      helper.errorResponse(
          "The [" + FILE_NAME_PARAM + "] parameter has not been specified.",
          responseFormat,
          parentService);
      return;
    }

    /* See if the client has specified a relative name... */

    File file;

    file = new File(startDirectory + File.separator + filename);

    log.info("***-> startDirectory = [" + file.getAbsolutePath() + "]");

    if (file.exists() == false) {
      helper.errorResponse(
          "The file [" + filename + "] could not be found.", responseFormat, parentService);
      return;
    }

    /* Check that the filename has a valid extension... */

    if (validExtensions.length > 0) {

      boolean valid = false;

      for (int idx = 0; idx < validExtensions.length; idx++) {
        if (filename.endsWith(validExtensions[idx].trim())) {
          valid = true;
          break;
        }
      }

      if (valid == false) {
        log.error("User tried to access a file [" + filename + "] with an invalid file extension.");
        helper.errorResponse(
            "The extension of the file you are trying to access is invalid.",
            responseFormat,
            parentService);
        return;
      }
    }

    /* Try to read the contents of the file into a string buffer... */

    StringBuffer content = new StringBuffer();

    try {
      FileInputStream fis = new FileInputStream(file);
      byte[] buf = new byte[1024];
      int len = 0;

      while ((len = fis.read(buf)) != -1) {
        content.append(new String(buf, 0, len));
      }

    } catch (Exception ex) {
      helper.errorResponse(
          "There was an exception reading the contents of the file ["
              + filename
              + "]: "
              + ex.toString(),
          responseFormat,
          parentService);
      return;
    }

    /** If the operation is to return RAW data then just write the file contents... */
    if (responseFormat.equals("RAW")) {
      helper.writeResponse(content.toString(), responseFormat, parentService);
      return;
    }

    /* Convert the contents into a base64 encoded string... */

    BASE64Encoder encoder = new BASE64Encoder();
    String encodedContent = encoder.encode(content.toString().getBytes());

    /* Build the response string... */

    StringBuffer responseString = new StringBuffer();

    responseString.append("<fileContents>");
    responseString.append(
        "<"
            + SetFileContents.FILENAME_ELEMENT
            + ">"
            + filename
            + "</"
            + SetFileContents.FILENAME_ELEMENT
            + ">");
    responseString.append(
        "<"
            + SetFileContents.ENCODEDCONTENT_ELEMENT
            + ">"
            + encodedContent
            + "</"
            + SetFileContents.ENCODEDCONTENT_ELEMENT
            + ">");
    responseString.append("</fileContents>");

    /* Send the list down to the client... */

    helper.writeResponse(responseString.toString(), responseFormat, parentService);

    return;
  }