Example usage for java.net URL getFile

List of usage examples for java.net URL getFile

Introduction

In this page you can find the example usage for java.net URL getFile.

Prototype

public String getFile() 

Source Link

Document

Gets the file name of this URL .

Usage

From source file:business.UploadTests.java

@Test(groups = "upload", dependsOnMethods = "createRequest")
public void uploadFileInvalidMimetype() throws IOException {
    UserAuthenticationToken requester = getRequester();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(requester);

    RequestRepresentation representation = requestController.getRequestById(requester, processInstanceId);
    log.info("Status: " + representation.getStatus());
    int attachmentCount = representation.getAttachments().size();

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Utrecht_Oude_Gracht_Hamburgerbrug_(LOC).jpg");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), resource.getFile().toString(), "undefined",
            input);/*w ww. ja  v  a 2  s .co  m*/

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    representation = requestController.uploadRequestAttachment(requester, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(attachmentCount + 1, representation.getAttachments().size());
    printFiles(representation.getAttachments());

    SecurityContextHolder.clearContext();
}

From source file:business.UploadTests.java

@Test(groups = "upload", dependsOnMethods = "createRequest")
public void uploadFileSuccess() throws IOException {
    UserAuthenticationToken requester = getRequester();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(requester);

    RequestRepresentation representation = requestController.getRequestById(requester, processInstanceId);
    log.info("Status: " + representation.getStatus());
    int attachmentCount = representation.getAttachments().size();

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Utrecht_Oude_Gracht_Hamburgerbrug_(LOC).jpg");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), resource.getFile().toString(), "image/jpeg",
            input);/*from w w  w .j  a v  a  2  s. co m*/

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    representation = requestController.uploadRequestAttachment(requester, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(attachmentCount + 1, representation.getAttachments().size());
    printFiles(representation.getAttachments());

    SecurityContextHolder.clearContext();
}

From source file:edu.du.penrose.systems.fedoraApp.batchIngest.data.BatchIngestXMLhandlerImpl.java

/**
 * Set the class variable xmlFileIterator based on files contained in a local directory.
 * /*from ww w  .  j  a  v  a2s .  co m*/
 * @param fileURL
 * @throws FatalException 
 */
private void setFileXMLiterator(URL fileURL) {

    String metsDirectoryName = fileURL.getFile().replace('/', File.separatorChar);

    metsDirectory = new FileComparator(metsDirectoryName);

    xmlFileIterator = Arrays.asList(metsDirectory.listFiles(this.myFileFilter)).iterator();

}

From source file:com.ning.billing.beatrix.osgi.SetupBundleWithAssertion.java

public void setupJrubyBundle() {

    try {//from   w w w.ja va2s .c  o m

        installJrubyJar();

        final URL resourceUrl = Resources.getResource(bundleName);
        final File unzippedRubyPlugin = unGzip(new File(resourceUrl.getFile()), rootInstallDir);

        final StringBuilder tmp = new StringBuilder(rootInstallDir.getAbsolutePath());
        tmp.append("/plugins/").append(PluginLanguage.RUBY.toString().toLowerCase());

        final File destination = new File(tmp.toString());
        if (!destination.exists()) {
            destination.mkdir();
        }

        unTar(unzippedRubyPlugin, destination);

    } catch (IOException e) {
        Assert.fail(e.getMessage());
    } catch (ArchiveException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:com.contentful.vaultintegration.BaseTest.java

protected void enqueue(String fileName) throws IOException {
    URL resource = getClass().getClassLoader().getResource(fileName);
    if (resource == null) {
        throw new IllegalArgumentException("File not found");
    }//  w w  w.  j a v  a 2s.c  om
    server.enqueue(new MockResponse().setResponseCode(200)
            .setBody(FileUtils.readFileToString(new File(resource.getFile()))));
}

From source file:edu.wfu.inotado.InotadoTestBase.java

/**
 * Reads a XML file from directory - inotado-api/api/resources/xml
 * //from   www.  ja v  a 2  s .  c  om
 * @param fileName
 */
@SuppressWarnings("resource")
protected String readXmlFromFile(String fileName) {
    BufferedReader br;
    String xmlStr = "No content available!";
    // get the file location
    URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
    String pathCurrent = location.getFile();
    String pathToXml = StringUtils.substringBefore(pathCurrent, "inotado-") + "/inotado-api/api/resources/xml/";
    String file = pathToXml + fileName;
    try {
        br = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = br.readLine();
        }
        xmlStr = sb.toString();
    } catch (FileNotFoundException e) {
        log.error("Unalbe to read the file:" + file, e);
    } catch (IOException e) {
        log.error("Erorr occurred while reading file:" + file, e);
    }
    return xmlStr;
}

From source file:edu.wfu.inotado.InotadoTestBase.java

/**
 * Reads a Json file from directory - inotado-api/api/resources/json
 * //from w  w  w  .j av a 2  s  . com
 * @param fileName
 */
@SuppressWarnings("resource")
protected String readJsonFromFile(String fileName) {
    BufferedReader br;
    String jsonStr = "No content available!";
    // get the file location
    URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
    String pathCurrent = location.getFile();
    String pathToXml = StringUtils.substringBefore(pathCurrent, "inotado-")
            + "/inotado-api/api/resources/json/";
    String file = pathToXml + fileName;
    try {
        br = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = br.readLine();
        }
        jsonStr = sb.toString();
    } catch (FileNotFoundException e) {
        log.error("Unalbe to read the file:" + file, e);
    } catch (IOException e) {
        log.error("Erorr occurred while reading file:" + file, e);
    }
    return jsonStr;
}

From source file:com.ebiznext.flume.source.TestMultiLineExecSource.java

@Test
public void testProcess() throws InterruptedException, LifecycleException, EventDeliveryException, IOException {
    Channel channel = new MemoryChannel();
    Context context = new Context();

    URL resource = Thread.currentThread().getContextClassLoader().getResource("server.log");
    assertNotNull(resource);/*w w  w.  ja  v  a  2s  .  com*/

    context.put("command", format("cat %s", resource.getFile()));
    //      context.put("regex", ".*(\\s*.*(Exception|Error): .+)|.*(\\s+at .+)|.*(\\s*Caused by:.+)|.*(\\s+... (\\d+) more)");
    context.put("keep-alive", "1");
    context.put("capacity", "100000");
    context.put("transactionCapacity", "100000");
    Configurables.configure(source, context);
    Configurables.configure(channel, context);

    ChannelSelector rcs = new ReplicatingChannelSelector();
    rcs.setChannels(Lists.newArrayList(channel));

    source.setChannelProcessor(new ChannelProcessor(rcs));

    source.start();
    Transaction transaction = channel.getTransaction();

    transaction.begin();
    Event event;

    File actualFile = new File("/tmp/flume-execsource." + Thread.currentThread().getId());
    FileOutputStream outputStream = new FileOutputStream(actualFile);

    while ((event = channel.take()) != null) {
        byte[] bytes = event.getBody();
        outputStream.write(bytes);
        outputStream.write('\n');
    }

    outputStream.close();
    transaction.commit();
    transaction.close();

    source.stop();

    File expectedFile = new File(resource.getFile());

    assertEquals(FileUtils.checksumCRC32(expectedFile), FileUtils.checksumCRC32(actualFile));

    FileUtils.forceDelete(actualFile);
}

From source file:de.jcup.egradle.eclipse.util.EclipseResourceHelper.java

public File getFileInPlugin(Plugin plugin, IPath path) throws CoreException {
    try {//from  w w  w . j  a  v  a 2  s.com
        URL installURL = plugin.getBundle().getEntry(path.toString());
        URL localURL = FileLocator.toFileURL(installURL);
        return new File(localURL.getFile());
    } catch (IOException e) {
        throw new PathException(IStatus.ERROR, path, "cannot get file in plugin", e);
    }
}

From source file:com.alfaariss.oa.engine.core.EngineLauncher.java

private Properties getConfigProperties() throws OAException {
    _logger.debug(/*  w  ww  .ja v  a  2s.com*/
            "Search for '" + PROPERTIES_FILENAME + "' as resource of the current thread context classloader");
    URL urlProperties = Thread.currentThread().getContextClassLoader().getResource(PROPERTIES_FILENAME);
    if (urlProperties != null) {
        String sProperties = urlProperties.getFile();
        _logger.debug("Found '" + PROPERTIES_FILENAME + "' file in: " + sProperties);
        File fProperties = new File(sProperties);
        if (fProperties != null && fProperties.exists()) {
            _logger.info("Updating configuration items with the items in: " + fProperties.getAbsolutePath());
            return getProperties(fProperties);
        }

        _logger.info("Could not resolve: " + fProperties.getAbsolutePath());
    } else
        _logger.info(
                "No '" + PROPERTIES_FILENAME + "' found as resource of the current thread context classloader");

    _logger.debug(
            "Search for '" + PROPERTIES_FILENAME + "' as resource of the classloader of the current class");
    urlProperties = EngineLauncher.class.getResource(PROPERTIES_FILENAME);
    if (urlProperties != null) {
        String sProperties = urlProperties.getFile();
        _logger.debug("Found '" + PROPERTIES_FILENAME + "' file in: " + sProperties);
        File fProperties = new File(sProperties);
        if (fProperties != null && fProperties.exists()) {
            _logger.info("Updating configuration items with the items in: " + fProperties.getAbsolutePath());
            return getProperties(fProperties);
        }

        _logger.info("Could not resolve: " + fProperties.getAbsolutePath());
    } else
        _logger.info(
                "No '" + PROPERTIES_FILENAME + "' found as resource of the classloader of the current class");

    _logger.debug("Search for '" + PROPERTIES_FILENAME + "' as system resource of the static classloader");
    urlProperties = ClassLoader.getSystemResource(PROPERTIES_FILENAME);
    if (urlProperties != null) {
        String sProperties = urlProperties.getFile();
        _logger.debug("Found '" + PROPERTIES_FILENAME + "' file in: " + sProperties);
        File fProperties = new File(sProperties);
        if (fProperties != null && fProperties.exists()) {
            _logger.info("Updating configuration items with the items in: " + fProperties.getAbsolutePath());
            return getProperties(fProperties);
        }

        _logger.info("Could not resolve: " + fProperties.getAbsolutePath());
    } else
        _logger.info("No '" + PROPERTIES_FILENAME + "' found as system resource of the static classloader");

    return null;
}