示例#1
0
 @Test
 public void testZipAndUnzipDirectory() throws IOException {
   File basedir = File.createTempFile(ZipUtils.class.getSimpleName(), ".tmp");
   basedir.delete();
   assertTrue("Could not create temporary base directory.", basedir.mkdir());
   for (int i = 0; i < 5; i++) {
     createSubDirWithFiles(basedir, i);
   }
   File zipFile = File.createTempFile(ZipUtils.class.getSimpleName(), ".zip");
   ZipArchiveOutputStream zipOutputStream =
       new ZipArchiveOutputStream(new FileOutputStream(zipFile));
   ZipUtils.addFilesToZipRecursively("mybase", basedir, null, zipOutputStream);
   zipOutputStream.close();
   final File unzipBase = File.createTempFile(ZipUtils.class.getSimpleName(), ".unzipped");
   ZipUtils.unzip(zipFile, unzipBase);
   IOUtils.processFiles(
       unzipBase,
       new FileProcessor() {
         public void process(File file) {
           try {
             assertEquals(
                 IOUtils.getFileIdentifier(unzipBase, file), FileUtils.readFileToString(file));
           } catch (IOException e) {
             throw new RuntimeException("Could not process file.", e);
           }
         }
       },
       null);
 }
  @Test
  public void testDeserializeZipArchiveSimple() throws Exception {
    // produce a zip archive containing a single serialized stream for this test.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(baos);
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(StreamId.APPLICATION_VERSION.name());
    zipOut.putArchiveEntry(zipEntry);
    IOUtils.copy(APPLICATION_VERSION_1.getInputStream(), zipOut);
    zipOut.closeArchiveEntry();
    zipOut.close();

    state = new PackageState();
    when(mockedMarshallerMap
            .get(StreamId.APPLICATION_VERSION)
            .getUnmarshaller()
            .unmarshal(any(Source.class)))
        .thenReturn(applicationVersion);
    ByteArrayInputStream zipIn = new ByteArrayInputStream(baos.toByteArray());
    underTest.deserialize(state, StreamId.APPLICATION_VERSION, zipIn);

    assertNotNull(state.getCreationToolVersion());
    assertEquals(applicationVersion, state.getCreationToolVersion());
    verify(mockedMarshallerMap.get(StreamId.APPLICATION_VERSION).getUnmarshaller())
        .unmarshal(any(Source.class));
  }
  /* package */ File createAndAddCustomizedAndroidManifestToSelendroidServer()
      throws IOException, ShellCommandException, AndroidSdkException {
    String targetPackageName = applicationUnderTest.getBasePackage();
    File tempdir =
        new File(
            FileUtils.getTempDirectoryPath()
                + File.separatorChar
                + targetPackageName
                + System.currentTimeMillis());

    if (!tempdir.exists()) {
      tempdir.mkdirs();
    }

    File customizedManifest = new File(tempdir, "AndroidManifest.xml");
    log.info(
        "Adding target package '"
            + targetPackageName
            + "' to "
            + customizedManifest.getAbsolutePath());

    // add target package
    InputStream inputStream = getResourceAsStream(selendroidApplicationXmlTemplate);
    if (inputStream == null) {
      throw new SelendroidException("AndroidApplication.xml template file was not found.");
    }
    String content = IOUtils.toString(inputStream, Charset.defaultCharset().displayName());

    // find the first occurance of "package" and appending the targetpackagename to begining
    int i = content.toLowerCase().indexOf("package");
    int cnt = 0;
    for (; i < content.length(); i++) {
      if (content.charAt(i) == '\"') {
        cnt++;
      }
      if (cnt == 2) {
        break;
      }
    }
    content = content.substring(0, i) + "." + targetPackageName + content.substring(i);
    log.info("Final Manifest File:\n" + content);
    content = content.replaceAll(SELENDROID_TEST_APP_PACKAGE, targetPackageName);
    // Seems like this needs to be done
    if (content.contains(ICON)) {
      content = content.replaceAll(ICON, "");
    }

    OutputStream outputStream = new FileOutputStream(customizedManifest);
    IOUtils.write(content, outputStream, Charset.defaultCharset().displayName());
    IOUtils.closeQuietly(inputStream);
    IOUtils.closeQuietly(outputStream);

    // adding the xml to an empty apk
    CommandLine createManifestApk = new CommandLine(AndroidSdk.aapt());

    createManifestApk.addArgument("package", false);
    createManifestApk.addArgument("-M", false);
    createManifestApk.addArgument(customizedManifest.getAbsolutePath(), false);
    createManifestApk.addArgument("-I", false);
    createManifestApk.addArgument(AndroidSdk.androidJar(), false);
    createManifestApk.addArgument("-F", false);
    createManifestApk.addArgument(
        tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk", false);
    createManifestApk.addArgument("-f", false);
    log.info(ShellCommand.exec(createManifestApk, 20000L));

    ZipFile manifestApk =
        new ZipFile(new File(tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk"));
    ZipArchiveEntry binaryManifestXml = manifestApk.getEntry("AndroidManifest.xml");

    File finalSelendroidServerFile = new File(tempdir.getAbsolutePath() + "selendroid-server.apk");
    ZipArchiveOutputStream finalSelendroidServer =
        new ZipArchiveOutputStream(finalSelendroidServerFile);
    finalSelendroidServer.putArchiveEntry(binaryManifestXml);
    IOUtils.copy(manifestApk.getInputStream(binaryManifestXml), finalSelendroidServer);

    ZipFile selendroidPrebuildApk = new ZipFile(selendroidServer.getAbsolutePath());
    Enumeration<ZipArchiveEntry> entries = selendroidPrebuildApk.getEntries();
    for (; entries.hasMoreElements(); ) {
      ZipArchiveEntry dd = entries.nextElement();
      finalSelendroidServer.putArchiveEntry(dd);

      IOUtils.copy(selendroidPrebuildApk.getInputStream(dd), finalSelendroidServer);
    }

    finalSelendroidServer.closeArchiveEntry();
    finalSelendroidServer.close();
    manifestApk.close();
    log.info("file: " + finalSelendroidServerFile.getAbsolutePath());
    return finalSelendroidServerFile;
  }
  private void start() throws Exception {
    super.dbInit();
    SystemConfigsEntity entity =
        SystemConfigsDao.get()
            .selectOnKey(SystemConfig.DATA_EXPORT, AppConfig.get().getSystemName());
    if (entity == null) {
      send("[Fail] create fail. please try again.");
      return;
    }
    // エクスポートデータを格納するディレクトリ
    AppConfig config = AppConfig.get();
    File base = new File(config.getTmpPath());
    File dir = new File(base, DATA_DIR);
    if (dir.exists()) {
      FileUtil.delete(dir);
    }
    dir.mkdirs();
    File attach = new File(dir, "attach");
    attach.mkdirs();
    File userdir = new File(dir, "user");
    userdir.mkdirs();

    // ナレッジデータを取得
    LoginedUser loginedUser =
        new LoginedUser() {
          @Override
          public boolean isAdmin() {
            return true;
          }
        };
    KnowledgeLogic knowledgeLogic = KnowledgeLogic.get();
    CommentsDao commentsDao = CommentsDao.get();
    KnowledgeFilesDao knowledgeFilesDao = KnowledgeFilesDao.get();

    List<KnowledgesEntity> knowledges = knowledgeLogic.searchKnowledge("", loginedUser, 0, 100);
    for (KnowledgesEntity knowledgesEntity : knowledges) {
      File f =
          new File(
              dir,
              "knowledge-"
                  + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6)
                  + ".xml");
      String xml = SerializeUtils.objectToXml(knowledgesEntity);
      FileUtil.write(f, xml);

      // ナレッジのコメントを取得
      List<CommentsEntity> comments =
          commentsDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId());
      for (CommentsEntity commentsEntity : comments) {
        File c =
            new File(
                dir,
                "comment-"
                    + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6)
                    + "-"
                    + StringUtils.zeroPadding(commentsEntity.getKnowledgeId(), 6)
                    + ".xml");
        xml = SerializeUtils.objectToXml(commentsEntity);
        FileUtil.write(c, xml);
      }
      List<KnowledgeFilesEntity> files =
          knowledgeFilesDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId());
      for (KnowledgeFilesEntity knowledgeFilesEntity : files) {
        KnowledgeFilesEntity file = knowledgeFilesDao.selectOnKey(knowledgeFilesEntity.getFileNo());
        File a =
            new File(
                attach,
                "attach-"
                    + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6)
                    + "-"
                    + StringUtils.zeroPadding(knowledgeFilesEntity.getFileNo(), 6)
                    + knowledgeFilesEntity.getFileName());
        OutputStream outputStream = new FileOutputStream(a);
        InputStream inputStream = file.getFileBinary();
        try {
          FileUtil.copy(inputStream, outputStream);
        } finally {
          inputStream.close();
          outputStream.close();
        }
      }
      send("[Export] knowledge-" + knowledgesEntity.getKnowledgeId());
    }

    // ユーザ情報を取得
    UsersDao usersDao = UsersDao.get();
    List<UsersEntity> users = usersDao.selectAll();
    for (UsersEntity usersEntity : users) {
      ExportUser user = new ExportUser();
      PropertyUtil.copyPropertyValue(usersEntity, user);
      File f = new File(userdir, "user-" + StringUtils.zeroPadding(user.getUserId(), 6) + ".xml");
      String xml = SerializeUtils.objectToXml(user);
      FileUtil.write(f, xml);
      send("[Export] user-" + user.getUserId());
    }

    // zip圧縮
    send("[Export] during the compression");
    String name = DATA_DIR + ".zip";
    File comp = new File(base, name);

    BufferedOutputStream output = null;
    ZipArchiveOutputStream os = null;
    try {
      output = new BufferedOutputStream(new FileOutputStream(comp));
      os = new ZipArchiveOutputStream(output);
      os.setEncoding("UTF-8");
      CompressLogic.get().addZip(os, dir, null);
    } finally {
      if (os != null) {
        os.close();
      }
      if (output != null) {
        output.close();
      }
    }
    send("[Export] complete!");
    // 圧縮の予約を削除
    SystemConfigsDao.get().physicalDelete(entity);
  }
示例#5
0
  private static byte[] getX509Zip(User u) throws Exception {
    X509Certificate cloudCert = null;
    final X509Certificate x509;
    String userAccessKey = null;
    String userSecretKey = null;
    KeyPair keyPair = null;
    try {
      for (AccessKey k : u.getKeys()) {
        if (k.isActive()) {
          userAccessKey = k.getAccessKey();
          userSecretKey = k.getSecretKey();
        }
      }
      if (userAccessKey == null) {
        AccessKey k = u.createKey();
        userAccessKey = k.getAccessKey();
        userSecretKey = k.getSecretKey();
      }
      keyPair = Certs.generateKeyPair();
      x509 = Certs.generateCertificate(keyPair, u.getName());
      x509.checkValidity();
      u.addCertificate(x509);
      cloudCert = SystemCredentials.lookup(Eucalyptus.class).getCertificate();
    } catch (Exception e) {
      LOG.fatal(e, e);
      throw e;
    }
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(byteOut);
    ZipArchiveEntry entry = null;
    String fingerPrint = Certs.getFingerPrint(keyPair.getPublic());
    if (fingerPrint != null) {
      String baseName =
          X509Download.NAME_SHORT
              + "-"
              + u.getName()
              + "-"
              + fingerPrint.replaceAll(":", "").toLowerCase().substring(0, 8);

      zipOut.setComment("To setup the environment run: source /path/to/eucarc");
      StringBuilder sb = new StringBuilder();
      // TODO:GRZE:FIXME velocity
      String userNumber = u.getAccount().getAccountNumber();
      sb.append("EUCA_KEY_DIR=$(dirname $(readlink -f ${BASH_SOURCE}))");
      if (Topology.isEnabled(Eucalyptus.class)) { // GRZE:NOTE: this is temporary
        sb.append(
            "\nexport EC2_URL=" + ServiceUris.remotePublicify(Topology.lookup(Eucalyptus.class)));
      } else {
        sb.append("\necho WARN:  Eucalyptus URL is not configured. >&2");
        ServiceBuilder<? extends ServiceConfiguration> builder =
            ServiceBuilders.lookup(Eucalyptus.class);
        ServiceConfiguration localConfig =
            builder.newInstance(
                Internets.localHostAddress(),
                Internets.localHostAddress(),
                Internets.localHostAddress(),
                Eucalyptus.INSTANCE.getPort());
        sb.append("\nexport EC2_URL=" + ServiceUris.remotePublicify(localConfig));
      }
      if (Topology.isEnabled(Walrus.class)) {
        ServiceConfiguration walrusConfig = Topology.lookup(Walrus.class);
        try {
          String uri = ServiceUris.remotePublicify(walrusConfig).toASCIIString();
          LOG.debug("Found walrus uri/configuration: uri=" + uri + " config=" + walrusConfig);
          sb.append("\nexport S3_URL=" + uri);
        } catch (Exception e) {
          LOG.error("Failed to set Walrus URL: " + walrusConfig, e);
        }
      } else {
        sb.append("\necho WARN:  Walrus URL is not configured. >&2");
      }
      // Disable notifications for now
      // sb.append( "\nexport AWS_SNS_URL=" + ServiceUris.remote( Notifications.class ) );
      if (Topology.isEnabled(Euare.class)) { // GRZE:NOTE: this is temporary
        sb.append("\nexport EUARE_URL=" + ServiceUris.remotePublicify(Euare.class));
      } else {
        sb.append("\necho WARN:  EUARE URL is not configured. >&2");
      }
      sb.append("\nexport EC2_PRIVATE_KEY=${EUCA_KEY_DIR}/" + baseName + "-pk.pem");
      sb.append("\nexport EC2_CERT=${EUCA_KEY_DIR}/" + baseName + "-cert.pem");
      sb.append("\nexport EC2_JVM_ARGS=-Djavax.net.ssl.trustStore=${EUCA_KEY_DIR}/jssecacerts");
      sb.append("\nexport EUCALYPTUS_CERT=${EUCA_KEY_DIR}/cloud-cert.pem");
      sb.append("\nexport EC2_ACCOUNT_NUMBER='" + u.getAccount().getAccountNumber() + "'");
      sb.append("\nexport EC2_ACCESS_KEY='" + userAccessKey + "'");
      sb.append("\nexport EC2_SECRET_KEY='" + userSecretKey + "'");
      sb.append("\nexport AWS_CREDENTIAL_FILE=${EUCA_KEY_DIR}/iamrc");
      sb.append("\nexport EC2_USER_ID='" + userNumber + "'");
      sb.append(
          "\nalias ec2-bundle-image=\"ec2-bundle-image --cert ${EC2_CERT} --privatekey ${EC2_PRIVATE_KEY} --user ${EC2_ACCOUNT_NUMBER} --ec2cert ${EUCALYPTUS_CERT}\"");
      sb.append(
          "\nalias ec2-upload-bundle=\"ec2-upload-bundle -a ${EC2_ACCESS_KEY} -s ${EC2_SECRET_KEY} --url ${S3_URL}\"");
      sb.append("\n");
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("eucarc"));
      entry.setUnixMode(0600);
      zipOut.write(sb.toString().getBytes("UTF-8"));
      zipOut.closeArchiveEntry();

      sb = new StringBuilder();
      sb.append("AWSAccessKeyId=").append(userAccessKey).append('\n');
      sb.append("AWSSecretKey=").append(userSecretKey);
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("iamrc"));
      entry.setUnixMode(0600);
      zipOut.write(sb.toString().getBytes("UTF-8"));
      zipOut.closeArchiveEntry();

      /** write the private key to the zip stream * */
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("cloud-cert.pem"));
      entry.setUnixMode(0600);
      zipOut.write(PEMFiles.getBytes(cloudCert));
      zipOut.closeArchiveEntry();

      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("jssecacerts"));
      entry.setUnixMode(0600);
      KeyStore tempKs = KeyStore.getInstance("jks");
      tempKs.load(null);
      tempKs.setCertificateEntry("eucalyptus", cloudCert);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      tempKs.store(bos, "changeit".toCharArray());
      zipOut.write(bos.toByteArray());
      zipOut.closeArchiveEntry();

      /** write the private key to the zip stream * */
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry(baseName + "-pk.pem"));
      entry.setUnixMode(0600);
      zipOut.write(PEMFiles.getBytes(keyPair.getPrivate()));
      zipOut.closeArchiveEntry();

      /** write the X509 certificate to the zip stream * */
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry(baseName + "-cert.pem"));
      entry.setUnixMode(0600);
      zipOut.write(PEMFiles.getBytes(x509));
      zipOut.closeArchiveEntry();
    }
    /** close the zip output stream and return the bytes * */
    zipOut.close();
    return byteOut.toByteArray();
  }