org.apache.cxf.helpers.FileUtils Java Examples

The following examples show how to use org.apache.cxf.helpers.FileUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WsdlOptionLoader.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static String[] readOptionsFromFile(File dir, String wsdlName) throws MojoExecutionException {
    String[] noOptions = new String[] {};
    List<File> files = FileUtils.getFiles(dir, wsdlName + WSDL_OPTIONS);
    if (files.isEmpty()) {
        return noOptions;
    }
    File optionsFile = files.iterator().next();
    if (optionsFile == null || !optionsFile.exists()) {
        return noOptions;
    }
    try {
        List<String> lines = FileUtils.readLines(optionsFile);
        if (lines.isEmpty()) {
            return noOptions;
        }
        return lines.iterator().next().split(" ");
    } catch (Exception e) {
        throw new MojoExecutionException("Error reading options from file "
                                         + optionsFile.getAbsolutePath(), e);
    }
}
 
Example #2
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodeGenNoIds3() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/resourcesNoId.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, "true");
    context.put(WadlToolConstants.CFG_INHERIT_PARAMS, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> javaFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".java");
    assertEquals(1, javaFiles.size());
    assertTrue(checkContains(javaFiles, "application.TestRsResource.java"));
    List<File> classFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(1, classFiles.size());
    assertTrue(checkContains(classFiles, "application.TestRsResource.class"));
}
 
Example #3
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodeGenNoIds2() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/multipleResources.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> javaFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".java");
    assertEquals(2, javaFiles.size());
    assertTrue(checkContains(javaFiles, "application.BookstoreResource.java"));
    assertTrue(checkContains(javaFiles, "application.BooksResource.java"));
    List<File> classFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(2, classFiles.size());
    assertTrue(checkContains(classFiles, "application.BookstoreResource.class"));
    assertTrue(checkContains(classFiles, "application.BooksResource.class"));
}
 
Example #4
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodeGenNoIds() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/singleResource.xml"));
    context.put(WadlToolConstants.CFG_RESOURCENAME, "CustomResource");
    context.put(WadlToolConstants.CFG_GENERATE_ENUMS, "true");
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> javaFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".java");
    assertEquals(2, javaFiles.size());
    assertTrue(checkContains(javaFiles, "application.CustomResource.java"));
    assertTrue(checkContains(javaFiles, "application.Theid.java"));

    List<File> classFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(2, classFiles.size());
    assertTrue(checkContains(classFiles, "application.CustomResource.class"));
    assertTrue(checkContains(classFiles, "application.Theid.class"));
}
 
Example #5
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodeGenWithResourceSet() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/singleResourceWithRefs.xml"));
    context.put(WadlToolConstants.CFG_RESOURCENAME, "CustomResource");
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> javaFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".java");
    assertEquals(1, javaFiles.size());
    assertTrue(checkContains(javaFiles, "application.CustomResource.java"));

    List<File> classFiles = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(1, classFiles.size());
    assertTrue(checkContains(classFiles, "application.CustomResource.class"));
}
 
Example #6
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodeGenHyphen() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/bookstoreHyphen.xml"));
    context.put(WadlToolConstants.CFG_IMPL, "true");
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(3, files.size());
    assertTrue(checkContains(files, "application" + ".BookstoreResource.class"));
    assertTrue(checkContains(files, "generated" + ".TestCompositeObject.class"));
    assertTrue(checkContains(files, "generated" + ".ObjectFactory.class"));
}
 
Example #7
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void verifyFiles(String ext, boolean subresourceExpected, boolean interfacesAndImpl,
                         String schemaPackage, String resourcePackage, int expectedCount,
                         boolean enumTypeExpected) {
    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, "." + ext);
    int offset = enumTypeExpected ? 1 : 2;
    int size = interfacesAndImpl ? expectedCount : expectedCount - offset;
    if (!subresourceExpected) {
        size--;
    }
    assertEquals(size, files.size());
    doVerifyTypes(files, schemaPackage, ext);
    if (subresourceExpected) {
        assertTrue(checkContains(files, resourcePackage + ".FormInterface." + ext));
        assertTrue(checkContains(files, resourcePackage + ".FormInterface2." + ext));
    }
    assertTrue(checkContains(files, resourcePackage + ".BookStore." + ext));
    if (interfacesAndImpl) {
        if (subresourceExpected) {
            assertTrue(checkContains(files, resourcePackage + ".FormInterfaceImpl." + ext));
            assertTrue(checkContains(files, resourcePackage + ".FormInterface2Impl." + ext));
        }
        assertTrue(checkContains(files, resourcePackage + ".BookStoreImpl." + ext));
    }
}
 
Example #8
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testResourceWithEPRNoSchemaGen() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/resourceWithEPR.xml"));
    context.put(WadlToolConstants.CFG_SCHEMA_TYPE_MAP,
        "{http://www.w3.org/2005/08/addressing}EndpointReferenceType="
        + "javax.xml.ws.wsaddressing.W3CEndpointReference");
    context.put(WadlToolConstants.CFG_NO_ADDRESS_BINDING, "true");
    context.put(WadlToolConstants.CFG_NO_TYPES, "true");

    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(1, files.size());
    assertTrue(checkContains(files, "application" + ".BookstoreResource.class"));
}
 
Example #9
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testResourceWithEPR() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/resourceWithEPR.xml"));
    context.put(WadlToolConstants.CFG_SCHEMA_TYPE_MAP,
                "{http://www.w3.org/2001/XMLSchema}date=javax.xml.datatype.XMLGregorianCalendar");
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(4, files.size());
    assertTrue(checkContains(files, "application" + ".BookstoreResource.class"));
    assertTrue(checkContains(files, "superbooks" + ".Book.class"));
    assertTrue(checkContains(files, "superbooks" + ".ObjectFactory.class"));
    assertTrue(checkContains(files, "superbooks" + ".package-info.class"));
}
 
Example #10
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void doTestInlinedSchemasWithImport(String loc) throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation(loc));
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(7, files.size());
    assertTrue(checkContains(files, "org.apache.cxf.jaxrs.model.wadl" + ".BookStore.class"));
    assertTrue(checkContains(files, "superbooks" + ".Book.class"));
    assertTrue(checkContains(files, "superbooks" + ".ObjectFactory.class"));
    assertTrue(checkContains(files, "superbooks" + ".package-info.class"));
    assertTrue(checkContains(files, "superchapters" + ".Chapter.class"));
    assertTrue(checkContains(files, "superchapters" + ".ObjectFactory.class"));
    assertTrue(checkContains(files, "superchapters" + ".package-info.class"));
}
 
Example #11
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodeGenInterfacesWithJaxbClassNameSuffix() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/bookstore.xml"));
    context.put(WadlToolConstants.CFG_JAXB_CLASS_NAME_SUFFIX, "DTO");
    context.put(WadlToolConstants.CFG_BINDING, getLocation("/wadl/jaxbSchemaBindings.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());
    List<File> schemafiles = FileUtils.getFilesRecurseUsingSuffix(output, ".java");
    assertEquals(10, schemafiles.size());
    doVerifyTypesWithSuffix(schemafiles, "superbooks", "java");

    List<File> classfiles = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(10, classfiles.size());
    doVerifyTypesWithSuffix(classfiles, "superbooks", "class");
}
 
Example #12
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInheritParameters() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/test.xml"));
    context.put(WadlToolConstants.CFG_COMPILE, "true");
    context.put(WadlToolConstants.CFG_SCHEMA_TYPE_MAP,
                "{http://www.w3.org/2001/XMLSchema}anyType="
                + "java.io.InputStream");
    context.put(WadlToolConstants.CFG_INHERIT_PARAMS, "last");
    context.put(WadlToolConstants.CFG_CREATE_JAVA_DOCS, "true");
    container.setContext(context);
    container.execute();

    assertNotNull(output.list());

    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(1, files.size());
}
 
Example #13
Source File: JAXRSContainerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoTargetNamespace() throws Exception {
    JAXRSContainer container = new JAXRSContainer(null);

    ToolContext context = new ToolContext();
    context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
    context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/resourceSchemaNoTargetNamespace.xml"));
    context.put(WadlToolConstants.CFG_SCHEMA_PACKAGENAME, "=custom");
    context.put(WadlToolConstants.CFG_COMPILE, "true");

    container.setContext(context);
    container.execute();

    assertNotNull(output.list());
    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, ".class");
    assertEquals(3, files.size());
    assertTrue(checkContains(files, "application" + ".Resource.class"));
    assertTrue(checkContains(files, "custom" + ".TestCompositeObject.class"));
    assertTrue(checkContains(files, "custom" + ".ObjectFactory.class"));
}
 
Example #14
Source File: WADLToJavaTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void verifyFiles(String ext, boolean subresourceExpected, boolean interfacesAndImpl,
                         String schemaPackage, String resourcePackage) {
    List<File> files = FileUtils.getFilesRecurseUsingSuffix(output, "." + ext);
    int size = interfacesAndImpl ? 11 : 10;
    if (!subresourceExpected) {
        size--;
    }
    assertEquals(size, files.size());
    doVerifyTypes(files, schemaPackage, ext);
    if (subresourceExpected) {
        assertTrue(checkContains(files, resourcePackage + ".FormInterface." + ext));
    }
    assertTrue(checkContains(files, resourcePackage + ".BookStore." + ext));
    if (interfacesAndImpl) {
        if (subresourceExpected) {
            assertTrue(checkContains(files, resourcePackage + ".FormInterfaceImpl." + ext));
        }
        assertTrue(checkContains(files, resourcePackage + ".BookStoreImpl." + ext));
    }
}
 
Example #15
Source File: AbstractCodegenMojo.java    From cxf with Apache License 2.0 6 votes vote down vote up
private String[] createForkOnceArgs(List<List<String>> wargs) throws MojoExecutionException {
    try {
        File f = FileUtils.createTempFile("cxf-w2j", "args");
        PrintWriter fw = new PrintWriter(new FileWriter(f));
        for (List<String> args : wargs) {
            fw.println(Integer.toString(args.size()));
            for (String s : args) {
                fw.println(s);
            }
        }
        fw.println("-1");
        fw.close();
        return new String[] {
            f.getAbsolutePath()
        };
    } catch (IOException ex) {
        throw new MojoExecutionException("Could not create argument file", ex);
    }
}
 
Example #16
Source File: AegisJaxWsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testBigList() throws Exception {
    int size = 1000;
    List<String> l = new ArrayList<>(size);
    for (int x = 0; x < size; x++) {
        l.add("Need to create a pretty big soap message to make sure we go over "
              + "some of the default buffer sizes and such so we can see what really"
              + "happens when we do that - " + x);
    }

    setupForTest(false);
    List<String> item = client.echoBigList(l);
    Assert.assertEquals(size, item.size());

    //CXF-2768
    File f = FileUtils.getDefaultTempDir();
    Assert.assertEquals(0, f.listFiles().length);
}
 
Example #17
Source File: AbstractCodeGeneratorMojo.java    From cxf with Apache License 2.0 6 votes vote down vote up
private String[] createForkOnceArgs(List<List<String>> wargs) throws MojoExecutionException {
    try {
        File f = FileUtils.createTempFile("cxf-w2j", "args");
        PrintWriter fw = new PrintWriter(new FileWriter(f));
        for (List<String> args : wargs) {
            fw.println(Integer.toString(args.size()));
            for (String s : args) {
                fw.println(s);
            }
        }
        fw.println("-1");
        fw.close();
        return new String[] {f.getAbsolutePath()};
    } catch (IOException ex) {
        throw new MojoExecutionException("Could not create argument file", ex);
    }
}
 
Example #18
Source File: OptionLoader.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static List<File> getWadlFiles(File dir, String[] includes, String[] excludes)
    throws MojoExecutionException {

    List<String> exList = new ArrayList<>();
    if (excludes != null) {
        Collections.addAll(exList, excludes);
    }
    Collections.addAll(exList, org.codehaus.plexus.util.FileUtils.getDefaultExcludes());

    String inc = joinWithComma(includes);
    String ex = joinWithComma(exList.toArray(new String[0]));

    try {
        List<?> newfiles = org.codehaus.plexus.util.FileUtils.getFiles(dir, inc, ex);
        return CastUtils.cast(newfiles);
    } catch (IOException exc) {
        throw new MojoExecutionException(exc.getMessage(), exc);
    }
}
 
Example #19
Source File: OptionLoader.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static String[] readOptionsFromFile(File dir, String wsdlName) throws MojoExecutionException {
    String[] noOptions = new String[] {};
    List<File> files = FileUtils.getFiles(dir, wsdlName + WADL_OPTIONS);
    if (files.isEmpty()) {
        return noOptions;
    }
    File optionsFile = files.iterator().next();
    if (optionsFile == null || !optionsFile.exists()) {
        return noOptions;
    }
    try {
        List<String> lines = FileUtils.readLines(optionsFile);
        if (lines.isEmpty()) {
            return noOptions;
        }
        return lines.iterator().next().split(" ");
    } catch (Exception e) {
        throw new MojoExecutionException("Error reading options from file "
                                         + optionsFile.getAbsolutePath(), e);
    }
}
 
Example #20
Source File: TrustedAuthorityValidatorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Before
public void setUpCertificateRepo() throws CertificateException {
    File storageDir = new File("target/teststore_trusted_authority_validator");
    FileUtils.removeDir(storageDir);
    storageDir.mkdirs();
    certificateRepo = new FileCertificateRepo("target/teststore_trusted_authority_validator");

    UseKeyWithType rootKey = new UseKeyWithType();
    rootKey.setApplication(Applications.PKIX.getUri());
    String subjectDN = certificateRoot.getSubjectX500Principal().getName();
    rootKey.setIdentifier(subjectDN);
    certificateRepo.saveTrustedCACertificate(certificateRoot, rootKey);

    UseKeyWithType aliceKey = new UseKeyWithType();
    aliceKey.setApplication(Applications.PKIX.getUri());
    subjectDN = certificateAlice.getSubjectX500Principal().getName();
    aliceKey.setIdentifier(subjectDN);
    certificateRepo.saveCACertificate(certificateAlice, aliceKey);
}
 
Example #21
Source File: SSLUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static TrustManager[] getDefaultTrustStoreManagers(Logger log) {
    String location = getTruststore(null, log);
    String trustStorePassword = getTruststorePassword(null, log);
    String trustStoreType = getTrustStoreType(null, log, DEFAULT_TRUST_STORE_TYPE);

    if (location != null) {
        File file = new File(location);
        try (InputStream is = FileUtils.exists(file) ? Files.newInputStream(file.toPath())
                : getResourceAsStream(location)) {
            if (is != null) {
                TrustManagerFactory tmf =
                    TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                KeyStore ks = KeyStore.getInstance(trustStoreType);

                ks.load(is, (trustStorePassword != null) ? trustStorePassword.toCharArray() : null);
                tmf.init(ks);
                return tmf.getTrustManagers();
            } else {
                log.log(Level.FINER, "No default trust keystore {0}", location);
            }
        } catch (Exception e) {
            log.log(Level.WARNING, "Default trust managers cannot be initialized: " + e.getMessage(), e);
        }
    }
    return null;
}
 
Example #22
Source File: SSLUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static synchronized void loadDefaultKeyManagers(Logger log) {
    if (defaultManagers != null) {
        return;
    }

    String location = getKeystore(null, log);
    String keyStorePassword = getKeystorePassword(null, log);
    String keyPassword = getKeyPassword(null, log);
    String keyStoreType = getKeystoreType(null, log);

    if (location != null) {
        File file = new File(location);
        try (InputStream is = FileUtils.exists(file) ? Files.newInputStream(file.toPath())
                : getResourceAsStream(location)) {
            if (is != null) {
                KeyManagerFactory kmf =
                    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                KeyStore ks = KeyStore.getInstance(keyStoreType != null ? keyStoreType : KeyStore.getDefaultType());
                ks.load(is, (keyStorePassword != null) ? keyStorePassword.toCharArray() : null);
                kmf.init(ks, (keyPassword != null) ? keyPassword.toCharArray() : null);
                defaultManagers = kmf.getKeyManagers();
            }
        } catch (Exception e) {
            log.log(Level.WARNING, "Default key managers cannot be initialized: " + e.getMessage(), e);
            defaultManagers = new KeyManager[0];
        }
    }
    if (null == defaultManagers) {
        log.log(Level.FINER, "No default keystore {0}", location);
        defaultManagers = new KeyManager[0];
    }
}
 
Example #23
Source File: Classes.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private static void doDump(final File base, final String name, final byte[] content) {
    final String target = "org/superbiz/app/" + name + ".class";
    final File targetFile = new File(base, target);
    FileUtils.mkDir(targetFile.getParentFile());
    try (final InputStream from = new ByteArrayInputStream(content);
         final OutputStream to = new FileOutputStream(targetFile)) {
        IO.copy(from, to);
    } catch (final IOException e) {
        fail();
    }
}
 
Example #24
Source File: CachedWriter.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void createFileOutputStream() throws IOException {
    if (tempFileFailed) {
        return;
    }
    LoadingCharArrayWriter bout = (LoadingCharArrayWriter)currentStream;
    try {
        if (outputDir == null) {
            tempFile = FileUtils.createTempFile("cos", "tmp");
        } else {
            tempFile = FileUtils.createTempFile("cos", "tmp", outputDir, false);
        }
        currentStream = createOutputStreamWriter(tempFile);
        bout.writeTo(currentStream);
        inmem = false;
        streamList.add(currentStream);
    } catch (Exception ex) {
        //Could be IOException or SecurityException or other issues.
        //Don't care what, just keep it in memory.
        tempFileFailed = true;
        if (currentStream != bout) {
            currentStream.close();
        }
        deleteTempFile();
        inmem = true;
        currentStream = bout;
    }
}
 
Example #25
Source File: CachedOutputStream.java    From cxf with Apache License 2.0 5 votes vote down vote up
private synchronized void deleteTempFile() {
    if (tempFile != null) {
        File file = tempFile;
        tempFile = null;
        FileUtils.delete(file);
    }
}
 
Example #26
Source File: CachedOutputStream.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void createFileOutputStream() throws IOException {
    if (tempFileFailed) {
        return;
    }
    ByteArrayOutputStream bout = (ByteArrayOutputStream)currentStream;
    try {
        if (outputDir == null) {
            tempFile = FileUtils.createTempFile("cos", "tmp");
        } else {
            tempFile = FileUtils.createTempFile("cos", "tmp", outputDir, false);
        }

        currentStream = createOutputStream(tempFile);
        bout.writeTo(currentStream);
        inmem = false;
        streamList.add(currentStream);
    } catch (Exception ex) {
        //Could be IOException or SecurityException or other issues.
        //Don't care what, just keep it in memory.
        tempFileFailed = true;
        if (currentStream != bout) {
            currentStream.close();
        }
        deleteTempFile();
        inmem = true;
        currentStream = bout;
    }
}
 
Example #27
Source File: Compiler.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void checkLongClasspath(String classpath, List<String> list, int classpathIdx) {
    if (isLongClasspath(classpath)) {
        try {
            classpathTmpFile = FileUtils.createTempFile("cxf-compiler-classpath", null);
            try (PrintWriter out = new PrintWriter(new FileWriter(classpathTmpFile))) {
                out.println(classpath);
                out.flush();
            }
            list.set(classpathIdx + 1, "@" + classpathTmpFile);
        } catch (IOException e) {
            System.err.print("[ERROR] can't write long classpath to @argfile");
        }
    }
}
 
Example #28
Source File: WsdlOptionLoader.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected static WsdlOption generateWsdlOptionFromFile(final File wsdl,
                                                       File defaultOutputDir)
    throws MojoExecutionException {

    if (wsdl == null || !wsdl.exists()) {
        return null;
    }

    final String wsdlFileName = wsdl.getName();
    int idx = wsdlFileName.toLowerCase().lastIndexOf(".wsdl");
    if (idx == -1) {
        idx = wsdlFileName.lastIndexOf('.');
    }
    if (idx == -1) {
        return null;
    }

    final WsdlOption wsdlOption = new WsdlOption();
    final String wsdlName = wsdlFileName.substring(0, idx);

    final String[] options = readOptionsFromFile(wsdl.getParentFile(), wsdlName);
    if (options.length > 0) {
        Collections.addAll(wsdlOption.getExtraargs(), options);
    }

    List<File> bindingFiles = FileUtils.getFiles(wsdl.getParentFile(), wsdlName + WSDL_BINDINGS);
    if (bindingFiles != null) {
        for (File binding : bindingFiles) {
            wsdlOption.addBindingFile(binding);
        }
    }
    wsdlOption.setWsdl(wsdl.toURI().toString());

    if (wsdlOption.getOutputDir() == null) {
        wsdlOption.setOutputDir(defaultOutputDir);
    }

    return wsdlOption;
}
 
Example #29
Source File: AttachmentUtil.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static Attachment createAttachment(InputStream stream, Map<String, List<String>> headers)
    throws IOException {

    String id = cleanContentId(getHeader(headers, "Content-ID"));

    AttachmentImpl att = new AttachmentImpl(id);

    final String ct = getHeader(headers, "Content-Type");
    String cd = getHeader(headers, "Content-Disposition");
    String fileName = getContentDispositionFileName(cd);

    String encoding = null;

    for (Map.Entry<String, List<String>> e : headers.entrySet()) {
        String name = e.getKey();
        if ("Content-Transfer-Encoding".equalsIgnoreCase(name)) {
            encoding = getHeader(headers, name);
            if ("binary".equalsIgnoreCase(encoding)) {
                att.setXOP(true);
            }
        }
        att.setHeader(name, getHeaderValue(e.getValue()));
    }
    if (encoding == null) {
        encoding = "binary";
    }
    InputStream ins = decode(stream, encoding);
    if (ins != stream) {
        headers.remove("Content-Transfer-Encoding");
    }
    DataSource source = new AttachmentDataSource(ct, ins);
    if (!StringUtils.isEmpty(fileName)) {
        ((AttachmentDataSource)source).setName(FileUtils.stripPath(fileName));
    }
    att.setDataHandler(new DataHandler(source));
    return att;
}
 
Example #30
Source File: BinaryDataProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public T readFrom(Class<T> clazz, Type genericType, Annotation[] annotations, MediaType type,
                       MultivaluedMap<String, String> headers, InputStream is)
    throws IOException {
    try {
        if (InputStream.class.isAssignableFrom(clazz)) {
            if (DigestInputStream.class.isAssignableFrom(clazz)) {
                is = new MessageDigestInputStream(is);
            }
            return clazz.cast(is);
        }
        if (Reader.class.isAssignableFrom(clazz)) {
            return clazz.cast(new InputStreamReader(is, getEncoding(type)));
        }
        if (byte[].class.isAssignableFrom(clazz)) {
            return clazz.cast(IOUtils.readBytesFromStream(is));
        }
        if (File.class.isAssignableFrom(clazz)) {
            LOG.warning("Reading data into File objects with the help of pre-packaged"
                + " providers is not recommended - use InputStream or custom File reader");
            // create a temp file, delete on exit
            File f = FileUtils.createTempFile("File" + UUID.randomUUID().toString(),
                                              "jaxrs",
                                              null,
                                              true);
            OutputStream os = Files.newOutputStream(f.toPath());
            IOUtils.copy(is, os, bufferSize);
            os.close();
            return clazz.cast(f);
        }
        if (StreamingOutput.class.isAssignableFrom(clazz)) {
            return clazz.cast(new ReadingStreamingOutput(is));
        }
    } catch (ClassCastException e) {
        String msg = "Unsupported class: " + clazz.getName();
        LOG.warning(msg);
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    throw new IOException("Unrecognized class");
}