List of usage examples for org.apache.commons.io.output NullOutputStream nullOutputStream
public static OutputStream nullOutputStream()
From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImplValuesTest.java
/** * Test method for {@link de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImpl#importAttributes(java.io.InputStream, java.io.PrintWriter)}. * @throws IOException if the test Excel file will be not found *///from www .j av a2 s . co m @Test public void testImportAttributesResponsibilityValuesWithWhitespaces() throws IOException { User alice = testDataHelper.createUser("alice", "", "", ""); User bob = testDataHelper.createUser("bob", "", "", ""); User joe = testDataHelper.createUser("joe", "", "", ""); UserGroup existinggroup = testDataHelper.createUserGroup("existinggroup"); UserGroup anotherexistinggroup = testDataHelper.createUserGroup("anotherexistinggroup"); PrintWriter logWriter = new PrintWriter(new NullOutputStream()); Resource excel = new ClassPathResource( EXCEL_TEST_FILES + "responsibilityAttributeValuesWithWhitespacesTest.xls"); excelAttributesImportService.importAttributes(excel.getInputStream(), logWriter); commit(); beginTransaction(); ResponsibilityAT responsibilityAt1 = (ResponsibilityAT) attributeTypeService .getAttributeTypeByName("ResponsibilityAt1"); assertNotNull(responsibilityAt1); List<ResponsibilityAV> responsibilityAV1 = responsibilityAt1.getSortedAttributeValues(); assertEquals(3, responsibilityAV1.size()); assertEquals(alice, responsibilityAV1.get(0).getUserEntity()); assertEquals(bob, responsibilityAV1.get(1).getUserEntity()); assertEquals(joe, responsibilityAV1.get(2).getUserEntity()); ResponsibilityAT responsibilityAt2 = (ResponsibilityAT) attributeTypeService .getAttributeTypeByName("ResponsibilityAt2"); assertNotNull(responsibilityAt2); List<ResponsibilityAV> responsibilityAV2 = responsibilityAt2.getSortedAttributeValues(); assertEquals(2, responsibilityAV2.size()); assertEquals(anotherexistinggroup, responsibilityAV2.get(0).getUserEntity()); assertEquals(existinggroup, responsibilityAV2.get(1).getUserEntity()); }
From source file:caarray.client.test.java.JavaApiFacade.java
public Long copyMageTabZipToOutputStream(String api, CaArrayEntityReference experimentReference, boolean compressed) throws Exception { CountingOutputStream outStream = new CountingOutputStream(new NullOutputStream()); dataApiUtils.copyMageTabZipToOutputStream(experimentReference, outStream); return outStream.getByteCount(); }
From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImplTest.java
/** * Test method for {@link de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImpl#importAttributes(java.io.InputStream, java.io.PrintWriter)}. * @throws IOException if the test Excel file will be not found *//* ww w .j a va 2s . c o m*/ @Test public void testImportAttributesRenameToBlank() throws IOException { AttributeTypeGroup atg = testDataHelper.createAttributeTypeGroup("testATG", "", Boolean.TRUE); EnumAT at = testDataHelper.createEnumAttributeType("Test", "old Description", Boolean.FALSE, atg); PrintWriter logWriter = new PrintWriter(new NullOutputStream()); Resource excel = new ClassPathResource(EXCEL_TEST_FILES + "enumAttributesRenameToBlankTest.xls"); excelAttributesImportService.importAttributes(excel.getInputStream(), logWriter); commit(); beginTransaction(); assertNotNull(attributeTypeService.getAttributeTypeByName("Test")); assertEquals("old Description", at.getDescription()); assertNull(attributeTypeService.getAttributeTypeByName("")); }
From source file:com.github.vatbub.awsvpnlauncher.Main.java
/** * Launches a new VPN server on AWS EC2 if everything is configured * * @see PropertyNotConfiguredException/*from ww w . j a v a 2 s . c om*/ * @see #terminate() */ private static void launch() { File privateKey = new File(prefs.getPreference(Property.privateKeyFile)); vpnPassword = prefs.getPreference(Property.openvpnPassword); if (!privateKey.exists() && !privateKey.isFile()) { throw new IllegalArgumentException("The file specified as " + Property.privateKeyFile.toString() + " does not exist or is not a file."); } FOKLogger.info(Main.class.getName(), "Preparing..."); try { // Check if our security group exists already FOKLogger.info(Main.class.getName(), "Checking for the required security group..."); DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest() .withGroupNames(securityGroupName); List<String> securityGroups = new ArrayList<>(); boolean created = false; // will become true if the security group had to be created to avoid duplicate logs String securityGroupId; try { DescribeSecurityGroupsResult describeSecurityGroupsResult = client .describeSecurityGroups(describeSecurityGroupsRequest); securityGroupId = describeSecurityGroupsResult.getSecurityGroups().get(0).getGroupId(); } catch (AmazonEC2Exception e) { // Security group does not exist, create the security group created = true; FOKLogger.info(Main.class.getName(), "Creating the required security group..."); CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest() .withGroupName(securityGroupName).withDescription( "This security group was automatically created to run a OpenVPN Access Server."); CreateSecurityGroupResult createSecurityGroupResult = client .createSecurityGroup(createSecurityGroupRequest); securityGroupId = createSecurityGroupResult.getGroupId(); IpRange ipRange = new IpRange().withCidrIp("0.0.0.0/0"); IpPermission sshPermission1 = new IpPermission().withIpv4Ranges(ipRange).withIpProtocol("tcp") .withFromPort(22).withToPort(22); IpPermission sshPermission2 = new IpPermission().withIpv4Ranges(ipRange).withIpProtocol("tcp") .withFromPort(943).withToPort(943); IpPermission httpsPermission1 = new IpPermission().withIpv4Ranges(ipRange).withIpProtocol("tcp") .withFromPort(443).withToPort(443); IpPermission httpsPermission2 = new IpPermission().withIpv4Ranges(ipRange).withIpProtocol("udp") .withFromPort(1194).withToPort(1194); AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest() .withGroupName(securityGroupName).withIpPermissions(sshPermission1) .withIpPermissions(sshPermission2).withIpPermissions(httpsPermission1) .withIpPermissions(httpsPermission2); // retry while the security group is not yet ready int retries = 0; long lastPollTime = System.currentTimeMillis(); boolean requestIsFailing = true; do { // we're waiting if (System.currentTimeMillis() - lastPollTime >= Math.pow(2, retries) * 100) { retries = retries + 1; lastPollTime = System.currentTimeMillis(); try { client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest); // no exception => we made it requestIsFailing = false; } catch (AmazonEC2Exception e2) { FOKLogger.info(Main.class.getName(), "Still waiting for the security group to be created, api error message is currently: " + e2.getMessage()); requestIsFailing = true; } } } while (requestIsFailing); FOKLogger.info(Main.class.getName(), "The required security group has been successfully created!"); } if (!created) { FOKLogger.info(Main.class.getName(), "The required security group already exists, we can continue"); } securityGroups.add(securityGroupId); securityGroups.add(securityGroupId); FOKLogger.info(Main.class.getName(), "Creating the RunInstanceRequest..."); RunInstancesRequest request = new RunInstancesRequest(getAmiId(awsRegion), 1, 1); request.setInstanceType(InstanceType.T2Micro); request.setKeyName(prefs.getPreference(Property.awsKeyPairName)); request.setSecurityGroupIds(securityGroups); FOKLogger.info(Main.class.getName(), "Starting the EC2 instance..."); RunInstancesResult result = client.runInstances(request); List<Instance> instances = result.getReservation().getInstances(); // SSH config FOKLogger.info(Main.class.getName(), "Configuring SSH..."); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); JSch jsch = new JSch(); jsch.addIdentity(privateKey.getAbsolutePath()); int retries = 0; for (Instance instance : instances) { // write the instance id to a properties file to be able to terminate it later on again prefs.reload(); if (prefs.getPreference("instanceIDs", "").equals("")) { prefs.setPreference("instanceIDs", instance.getInstanceId()); } else { prefs.setPreference("instanceIDs", prefs.getPreference("instanceIDs", "") + ";" + instance.getInstanceId()); } // Connect to the instance using ssh FOKLogger.info(Main.class.getName(), "Waiting for the instance to boot..."); long lastPrintTime = System.currentTimeMillis(); DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest(); List<String> instanceId = new ArrayList<>(1); instanceId.add(instance.getInstanceId()); describeInstancesRequest.setInstanceIds(instanceId); DescribeInstancesResult describeInstancesResult; newInstance = instance; do { // we're waiting if (System.currentTimeMillis() - lastPrintTime >= Math.pow(2, retries) * 100) { retries = retries + 1; describeInstancesResult = client.describeInstances(describeInstancesRequest); newInstance = describeInstancesResult.getReservations().get(0).getInstances().get(0); lastPrintTime = System.currentTimeMillis(); if (newInstance.getState().getCode() != 16) { FOKLogger.info(Main.class.getName(), "Still waiting for the instance to boot, current instance state is " + newInstance.getState().getName()); } } } while (newInstance.getState().getCode() != 16); FOKLogger.info(Main.class.getName(), "Instance is " + newInstance.getState().getName()); // generate the ssh ip of the instance String sshIp = newInstance.getPublicDnsName(); FOKLogger.info(Main.class.getName(), "The instance id is " + newInstance.getInstanceId()); FOKLogger.info(Main.class.getName(), "The instance ip is " + newInstance.getPublicIpAddress()); FOKLogger.info(Main.class.getName(), "Connecting using ssh to " + sshUsername + "@" + sshIp); FOKLogger.info(Main.class.getName(), "The instance will need some time to configure ssh on its end so some connection timeouts are normal"); boolean retry; session = jsch.getSession(sshUsername, sshIp, 22); session.setConfig(sshConfig); do { try { session.connect(); retry = false; } catch (Exception e) { FOKLogger.info(Main.class.getName(), e.getClass().getName() + ": " + e.getMessage() + ", retrying, Press Ctrl+C to cancel"); retry = true; } } while (retry); FOKLogger.info(Main.class.getName(), "----------------------------------------------------------------------"); FOKLogger.info(Main.class.getName(), "The following is the out- and input of the ssh session."); FOKLogger.info(Main.class.getName(), "Please note that out- and input may appear out of sync."); FOKLogger.info(Main.class.getName(), "----------------------------------------------------------------------"); PipedInputStream sshIn = new PipedInputStream(); PipedOutputStream sshIn2 = new PipedOutputStream(sshIn); PrintStream sshInCommandStream = new PrintStream(sshIn2); Channel channel = session.openChannel("shell"); channel.setInputStream(sshIn); channel.setOutputStream(new MyPrintStream()); channel.connect(); sshInCommandStream.print("yes\n"); sshInCommandStream.print("yes\n"); sshInCommandStream.print("1\n"); sshInCommandStream.print("\n"); sshInCommandStream.print("\n"); sshInCommandStream.print("yes\n"); sshInCommandStream.print("yes\n"); sshInCommandStream.print("\n"); sshInCommandStream.print("\n"); sshInCommandStream.print("\n"); sshInCommandStream.print("\n"); sshInCommandStream.print("echo \"" + adminUsername + ":" + vpnPassword + "\" | sudo chpasswd\n"); sshInCommandStream.print("exit\n"); NullOutputStream nullOutputStream = new NullOutputStream(); Thread watchForSSHDisconnectThread = new Thread(() -> { while (channel.isConnected()) { nullOutputStream.write(0); } // disconnected cont(); }); watchForSSHDisconnectThread.setName("watchForSSHDisconnectThread"); watchForSSHDisconnectThread.start(); } } catch (JSchException | IOException e) { e.printStackTrace(); if (session != null) { session.disconnect(); } System.exit(1); } }
From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImplTest.java
/** * Test method for {@link de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImpl#importAttributes(java.io.InputStream, java.io.PrintWriter)}. * @throws IOException if the test Excel file will be not found *//*from w w w . j a v a 2 s . co m*/ @Test public void testImportAttributesDouble() throws IOException { PrintWriter logWriter = new PrintWriter(new NullOutputStream()); Resource excel = new ClassPathResource(EXCEL_TEST_FILES + "enumAttributesDoubleTest.xls"); excelAttributesImportService.importAttributes(excel.getInputStream(), logWriter); commit(); beginTransaction(); EnumAT enumAt1 = (EnumAT) attributeTypeService.getAttributeTypeByName("enumAt1"); assertNotNull(enumAt1); assertEquals("d3", enumAt1.getDescription()); List<EnumAV> attributeValues1 = enumAt1.getSortedAttributeValues(); assertEquals(1, attributeValues1.size()); assertEquals("testvalue3", attributeValues1.get(0).getName()); EnumAT enumAt2 = (EnumAT) attributeTypeService.getAttributeTypeByName("enumAt2"); assertNotNull(enumAt2); assertEquals("d2", enumAt2.getDescription()); List<EnumAV> attributeValues2 = enumAt2.getSortedAttributeValues(); assertEquals(2, attributeValues2.size()); assertEquals("testvalue1", attributeValues2.get(0).getName()); assertEquals("testvalue2", attributeValues2.get(1).getName()); }
From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImplValuesTest.java
/** * Test method for {@link de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImpl#importAttributes(java.io.InputStream, java.io.PrintWriter)}. * @throws IOException if the test Excel file will be not found *///from ww w. j av a2 s. c o m @Test public void testImportAttributesRange() throws IOException { PrintWriter logWriter = new PrintWriter(new NullOutputStream()); Resource excel = new ClassPathResource(EXCEL_TEST_FILES + "numberAttributesRangeTest.xls"); excelAttributesImportService.importAttributes(excel.getInputStream(), logWriter); commit(); beginTransaction(); NumberAT numberAt1 = (NumberAT) attributeTypeService.getAttributeTypeByName("NumberAt1"); assertNotNull(numberAt1); assertEquals(true, numberAt1.isRangeUniformyDistributed()); assertTrue(numberAt1.getRangeValues().isEmpty()); NumberAT numberAt2 = (NumberAT) attributeTypeService.getAttributeTypeByName("NumberAt2"); assertNotNull(numberAt2); assertEquals(false, numberAt2.isRangeUniformyDistributed()); assertEquals(new BigDecimal("123.00"), numberAt2.getRangeValues().iterator().next().getValue()); }
From source file:hudson.ClassicPluginStrategy.java
/** * Repackage classes directory into a jar file to make it remoting friendly. * The remoting layer can cache jar files but not class files. *///from w ww. j a v a2s . co m private static void createClassJarFromWebInfClasses(File archive, File destDir, Project prj) throws IOException { File classesJar = new File(destDir, "WEB-INF/lib/classes.jar"); ZipFileSet zfs = new ZipFileSet(); zfs.setProject(prj); zfs.setSrc(archive); zfs.setIncludes("WEB-INF/classes/"); MappedResourceCollection mapper = new MappedResourceCollection(); mapper.add(zfs); GlobPatternMapper gm = new GlobPatternMapper(); gm.setFrom("WEB-INF/classes/*"); gm.setTo("*"); mapper.add(gm); final long dirTime = archive.lastModified(); // this ZipOutputStream is reused and not created for each directory final ZipOutputStream wrappedZOut = new ZipOutputStream(new NullOutputStream()) { @Override public void putNextEntry(ZipEntry ze) throws IOException { ze.setTime(dirTime + 1999); // roundup super.putNextEntry(ze); } }; try { Zip z = new Zip() { /** * Forces the fixed timestamp for directories to make sure * classes.jar always get a consistent checksum. */ protected void zipDir(Resource dir, ZipOutputStream zOut, String vPath, int mode, ZipExtraField[] extra) throws IOException { // use wrappedZOut instead of zOut super.zipDir(dir, wrappedZOut, vPath, mode, extra); } }; z.setProject(prj); z.setTaskType("zip"); classesJar.getParentFile().mkdirs(); z.setDestFile(classesJar); z.add(mapper); z.execute(); } finally { wrappedZOut.close(); } }
From source file:de.blizzy.backup.backup.BackupRun.java
private String getChecksum(IFile file) throws IOException { final MessageDigest[] digest = new MessageDigest[1]; IOutputStreamProvider outputStreamProvider = new IOutputStreamProvider() { @Override//from ww w.j a va2s .c o m public OutputStream getOutputStream() { try { digest[0] = MessageDigest.getInstance("SHA-256"); //$NON-NLS-1$ return new DigestOutputStream(new NullOutputStream(), digest[0]); } catch (GeneralSecurityException e) { throw new RuntimeException(e); } } }; file.copy(outputStreamProvider); return toHexString(digest[0]); }
From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImplTest.java
/** * Test method for {@link de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImpl#importAttributes(java.io.InputStream, java.io.PrintWriter)}. * @throws IOException if the test Excel file will be not found *//*from w w w. java2 s . c o m*/ @Test public void testImportAttributesGroup() throws IOException { AttributeTypeGroup atg = testDataHelper.createAttributeTypeGroup("testATG", "", Boolean.TRUE); PrintWriter logWriter = new PrintWriter(new NullOutputStream()); Resource excel = new ClassPathResource(EXCEL_TEST_FILES + "enumAttributesGroupTest.xls"); excelAttributesImportService.importAttributes(excel.getInputStream(), logWriter); commit(); beginTransaction(); EnumAT enumAt = (EnumAT) attributeTypeService.getAttributeTypeByName("Test"); assertNotNull(enumAt); assertEquals(atg, enumAt.getAttributeTypeGroup()); }
From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImplValuesTest.java
/** * Test method for {@link de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelAttributesImportServiceImpl#importAttributes(java.io.InputStream, java.io.PrintWriter)}. * @throws IOException if the test Excel file will be not found *//*from www .j ava2 s. co m*/ @Test public void testImportAttributesRangeUpdate() throws IOException { NumberAT numberAt1 = testDataHelper.createNumberAttributeType("NumberAt1", "", attributeTypeGroupService.getStandardAttributeTypeGroup()); testDataHelper.createRangeValue(new BigDecimal("123"), numberAt1); NumberAT numberAt2 = testDataHelper.createNumberAttributeType("NumberAt2", "", attributeTypeGroupService.getStandardAttributeTypeGroup()); testDataHelper.createRangeValue(new BigDecimal("123"), numberAt2); testDataHelper.createRangeValue(new BigDecimal("999"), numberAt2); commit(); beginTransaction(); PrintWriter logWriter = new PrintWriter(new NullOutputStream()); Resource excel = new ClassPathResource(EXCEL_TEST_FILES + "numberAttributesRangeTest.xls"); excelAttributesImportService.importAttributes(excel.getInputStream(), logWriter); commit(); beginTransaction(); NumberAT numberAt1Reloaded = attributeTypeService.loadObjectById(numberAt1.getId(), NumberAT.class); NumberAT numberAt2Reloaded = attributeTypeService.loadObjectById(numberAt2.getId(), NumberAT.class); assertNotNull(numberAt1Reloaded); assertEquals(true, numberAt1Reloaded.isRangeUniformyDistributed()); assertEquals(1, numberAt1Reloaded.getRangeValues().size()); assertEquals(new BigDecimal("123"), numberAt1Reloaded.getRangeValues().iterator().next().getValue()); assertNotNull(numberAt2Reloaded); assertEquals(false, numberAt2Reloaded.isRangeUniformyDistributed()); assertEquals(1, numberAt2Reloaded.getRangeValues().size()); assertEquals(new BigDecimal("123.00"), numberAt2Reloaded.getRangeValues().iterator().next().getValue()); }