private long getPid(File nodeDir) throws IOException {
    File binDir = new File(nodeDir, "bin");
    StringWriter writer = new StringWriter();
    StreamUtil.copy(new FileReader(new File(binDir, "cassandra.pid")), writer);

    return Long.parseLong(writer.getBuffer().toString());
  }
  private void updateStorageAuthConf(File basedir) {
    File confDir = new File(basedir, "conf");
    File authFile = new File(confDir, "rhq-storage-auth.conf");
    authFile.delete();

    Set<String> addresses = calculateLocalIPAddresses(deploymentOptions.getNumNodes());

    try {
      StreamUtil.copy(
          new StringReader(StringUtil.collectionToString(addresses, "\n")),
          new FileWriter(authFile),
          true);
    } catch (IOException e) {
      throw new RuntimeException("Failed to update " + authFile);
    }
  }
  private BundleVersion getBundleVersion(Bundle bundle) throws Exception {
    BundleVersionCriteria criteria = new BundleVersionCriteria();
    criteria.addFilterBundleId(bundle.getId());
    criteria.addFilterVersion(bundleVersion);
    criteria.fetchBundle(true);
    criteria.fetchBundleDeployments(true);

    PageList<BundleVersion> bundleVersions =
        bundleManager.findBundleVersionsByCriteria(overlord, criteria);

    if (bundleVersions.isEmpty()) {
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      StreamUtil.copy(getClass().getResourceAsStream("cassandra-bundle.jar"), outputStream);
      return bundleManager.createBundleVersionViaByteArray(overlord, outputStream.toByteArray());
    }

    return bundleVersions.get(0);
  }
예제 #4
0
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    HttpSession session = req.getSession();
    session.setMaxInactiveInterval(MAX_INACTIVE_INTERVAL);

    if (ServletFileUpload.isMultipartContent(req)) {

      DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
      // fileItemFactory.setSizeThreshold(0);

      ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

      List<FileItem> fileItemsList;
      try {
        fileItemsList = (List<FileItem>) servletFileUpload.parseRequest(req);
      } catch (FileUploadException e) {
        writeExceptionResponse(resp, "File upload failed", e);
        return;
      }

      List<FileItem> actualFiles = new ArrayList<FileItem>();
      Map<String, String> formFields = new HashMap<String, String>();
      boolean retrieve = false;
      Subject authenticatedSubject = null;

      for (FileItem fileItem : fileItemsList) {
        if (fileItem.isFormField()) {
          if (fileItem.getFieldName() != null) {
            formFields.put(fileItem.getFieldName(), fileItem.getString());
          }
          if ("retrieve".equals(fileItem.getFieldName())) {
            retrieve = true;
          } else if ("sessionid".equals(fileItem.getFieldName())) {
            int sessionid = Integer.parseInt(fileItem.getString());
            SubjectManagerLocal subjectManager = LookupUtil.getSubjectManager();
            try {
              authenticatedSubject = subjectManager.getSubjectBySessionId(sessionid);
            } catch (Exception e) {
              throw new ServletException("Cannot authenticate request", e);
            }
          }
          fileItem.delete();
        } else {
          // file item contains an actual uploaded file
          actualFiles.add(fileItem);
          log("file was uploaded: " + fileItem.getName());
        }
      }

      if (authenticatedSubject == null) {
        for (FileItem fileItem : actualFiles) {
          fileItem.delete();
        }
        throw new ServletException("Cannot process unauthenticated request");
      }

      if (retrieve && actualFiles.size() == 1) {
        // sending in "retrieve" form element with a single file means the client just wants the
        // content echoed back
        resp.setContentType("text/html");
        FileItem fileItem = actualFiles.get(0);

        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.print("<html>");
        InputStream inputStream = fileItem.getInputStream();
        try {
          // we have to HTML escape inputStream before writing it to outputStream
          StreamUtil.copy(inputStream, outputStream, false, true);
        } finally {
          inputStream.close();
        }
        outputStream.print("</html>");
        outputStream.flush();

        fileItem.delete();
      } else {
        Map<String, File> allUploadedFiles =
            new HashMap<String, File>(); // maps form field name to the actual file
        Map<String, String> allUploadedFileNames =
            new HashMap<String, String>(); // maps form field name to upload file name
        for (FileItem fileItem : actualFiles) {
          File theFile = forceToFile(fileItem);
          allUploadedFiles.put(fileItem.getFieldName(), theFile);
          allUploadedFileNames.put(
              fileItem.getFieldName(),
              (fileItem.getName() != null) ? fileItem.getName() : theFile.getName());
        }
        processUploadedFiles(
            authenticatedSubject, allUploadedFiles, allUploadedFileNames, formFields, req, resp);
      }
    }
  }