Exemplo n.º 1
0
  private void convertToLocalTempDataBean(DataBean bean) throws IOException {

    try {
      // copy contents to new file
      File newFile = this.createNewRepositoryFile(bean.getName());
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));
      ChecksumInputStream inputStream =
          getContentStream(bean, DataNotAvailableHandling.EXCEPTION_ON_NA);
      BufferedInputStream in = new BufferedInputStream(inputStream);
      try {
        IOUtils.copy(in, out);

        setOrVerifyChecksum(bean, inputStream.verifyChecksums());
      } finally {
        IOUtils.closeIfPossible(in);
        IOUtils.closeIfPossible(out);
      }

      // update url, type and handler in the bean
      URL newURL = newFile.toURI().toURL();
      addContentLocationForDataBean(bean, StorageMethod.LOCAL_TEMP, newURL);
    } catch (ChecksumException | ContentLengthException e) {
      // corrupted data
      throw new IOException();
    }
  }
Exemplo n.º 2
0
  /**
   * Convenience method for creating a local temporary file DataBean with content. Content stream is
   * read into a temp file and location of the file is stored to DataBean.
   *
   * @throws IOException
   */
  public DataBean createDataBean(String name, InputStream content)
      throws MicroarrayException, IOException {

    // copy the data from the input stream to the file in repository
    File contentFile;
    contentFile = createNewRepositoryFile(name);
    InputStream input = new BufferedInputStream(content);
    OutputStream output = new BufferedOutputStream(new FileOutputStream(contentFile));
    try {
      IO.copy(input, output);
      output.flush();
    } finally {
      IOUtils.closeIfPossible(input);
      IOUtils.closeIfPossible(output);
    }

    // create and return the bean
    DataBean bean = createDataBean(name);
    try {
      addContentLocationForDataBean(bean, StorageMethod.LOCAL_TEMP, contentFile.toURI().toURL());
    } catch (MalformedURLException e) {
      throw new MicroarrayException(e);
    } catch (ContentLengthException e) {
      // shouldn't happen, because newly created bean doesn't have size set
      logger.error(e, e);
    }
    return bean;
  }
  /**
   * Get a local copy of a file. If the dataId matches any of the files found from local filebroker
   * paths (given in constructor of this class), then it is symlinked or copied locally. Otherwise
   * the file pointed by the dataId is downloaded.
   *
   * @throws JMSException
   * @throws ChecksumException
   * @see fi.csc.microarray.filebroker.FileBrokerClient#getFile(File, URL)
   */
  @Override
  public void getFile(UUID sessionId, String dataId, File destFile)
      throws IOException, FileBrokerException, ChecksumException {

    InputStream inStream = download(sessionId, dataId);
    IOUtils.copy(inStream, destFile);
  }
Exemplo n.º 4
0
  private void parseMetadata() throws ZipException, IOException, JAXBException, SAXException {
    ZipFile zipFile = null;
    try {
      // get the session.xml zip entry
      zipFile = new ZipFile(sessionFile);
      InputStream metadataStream =
          zipFile.getInputStream(zipFile.getEntry(UserSession.SESSION_DATA_FILENAME));

      // validate
      // ClientSession.getSchema().newValidator().validate(new StreamSource(metadataStream));

      // parse the metadata xml to java objects using jaxb
      Unmarshaller unmarshaller = UserSession.getPreviousJAXBContext().createUnmarshaller();
      unmarshaller.setSchema(UserSession.getPreviousSchema());
      NonStoppingValidationEventHandler validationEventHandler =
          new NonStoppingValidationEventHandler();
      unmarshaller.setEventHandler(validationEventHandler);
      this.sessionType =
          unmarshaller.unmarshal(new StreamSource(metadataStream), SessionType.class).getValue();

      if (validationEventHandler.hasEvents()) {
        throw new JAXBException(
            "Invalid session file:\n" + validationEventHandler.getValidationEventsAsString());
      }
    } finally {
      IOUtils.closeIfPossible(zipFile);
    }
  }
Exemplo n.º 5
0
  private String getSourceCode(String sourceCodeFileName) throws ZipException, IOException {
    ZipFile zipFile = null;
    InputStream sourceCodeInputStream = null;
    StringWriter stringWriter = null;
    try {
      zipFile = new ZipFile(sessionFile);
      sourceCodeInputStream = zipFile.getInputStream(zipFile.getEntry(sourceCodeFileName));

      stringWriter = new StringWriter();
      IOUtils.copy(sourceCodeInputStream, new WriterOutputStream(stringWriter));
      stringWriter.flush();
    } finally {
      IOUtils.closeIfPossible(sourceCodeInputStream);
      IOUtils.closeIfPossible(stringWriter);
      if (zipFile != null) {
        zipFile.close();
      }
    }

    return stringWriter.toString();
  }
  @Override
  public boolean isAvailable(
      String dataId, Long contentLength, String checksum, FileBrokerArea area)
      throws FileBrokerException {

    try {
      InputStream inStream = download(getSessionId(), dataId);
      IOUtils.closeIfPossible(inStream);
      return true;
    } catch (NotFoundException e) {
      return false;
    }
  }
Exemplo n.º 7
0
  /**
   * A convenience method for gathering streamed binary content into a byte array. Gathers only
   * maxLength first bytes. Returns null if none of the content locations are available.
   *
   * @throws IOException
   * @see #getContentStream()
   */
  public byte[] getContentBytes(DataBean bean, long maxLength, DataNotAvailableHandling naHandling)
      throws IOException {

    InputStream in = null;
    try {
      in = getContentStream(bean, naHandling);
      if (in != null) {
        return Files.inputStreamToBytes(in, maxLength);

      } else {
        return null;
      }

    } finally {
      IOUtils.closeIfPossible(in);
    }
  }