Example usage for org.springframework.core.io ClassPathResource exists

List of usage examples for org.springframework.core.io ClassPathResource exists

Introduction

In this page you can find the example usage for org.springframework.core.io ClassPathResource exists.

Prototype

@Override
public boolean exists() 

Source Link

Document

This implementation checks for the resolution of a resource URL.

Usage

From source file:ru.anr.base.tests.GlassfishLoader.java

/**
 * Executes a file with asadmin commands
 * //  ww  w . ja  v  a2s.  co  m
 * @param fileName
 *            Name of a file
 * @throws IOException
 *             In case of File not found
 * @throws GlassFishException
 *             In case of Glassfish error
 */
private void executeScript(String fileName) throws IOException, GlassFishException {

    ClassPathResource script = new ClassPathResource(fileName);

    if (script.exists()) {

        List<String> lines = IOUtils.readLines(script.getInputStream());
        CommandRunner cmd = glassfish.getCommandRunner();

        for (String l : lines) {

            String[] tokens = translateCommandline(l);

            if (tokens.length == 0 || "#".equals(tokens[0])) {
                continue;
            }

            String[] args = ArrayUtils.subarray(tokens, 1, tokens.length);

            logger.info("Runnning a command: {} {}", tokens[0], args);
            CommandResult r = cmd.run(tokens[0], args);

            switch (r.getExitStatus()) {
            case SUCCESS:
                logger.info("SUCCESS >> {}", r.getOutput());
                break;
            case WARNING:
                logger.info("WARNING >> {} ( {} )", r.getOutput(), r.getFailureCause());
                break;
            case FAILURE:
                logger.info("ERROR >> \n{}", r.getOutput(), r.getFailureCause());
                throw new ApplicationException(r.getFailureCause());
            default:
            }
        }
    } else {
        logger.info("'glassfish.txt' file not found");
    }
}

From source file:nl.surfnet.coin.api.service.MockServiceImpl.java

@Override
public PersonEntry getPerson(String userId, String loggedInUser, String spEntityId) {
    if (isActive()) {
        return getPreparedPerson(userId);
    }/* ww w .j av  a  2  s.com*/
    /*
     * Strip all characters that might cause problems in filenames. e.g.:
     * urn:collab:person:test.surfguest.nl:foo becomes:
     * urn_collab_person_test.surfguest.nl_foo
     */
    String userIdStripped = userId.replaceAll("[^0-9a-zA-Z_.-]", "_");

    final String filename = String.format(JSON_PATH, userIdStripped, "person");
    LOG.debug("filename: {}", filename);
    ClassPathResource pathResource = new ClassPathResource(filename);
    if (!pathResource.exists()) {
        pathResource = new ClassPathResource(String.format(JSON_PATH, FALLBACK, "person"));
    }
    try {
        return parser.parsePerson(pathResource.getInputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:nl.surfnet.coin.api.service.MockServiceImpl.java

@Override
public GroupEntry getGroups(String userId, String onBehalfOf, Integer count, Integer startIndex,
        String sortBy) {/*from  w w  w  .  java 2  s .c o  m*/
    ClassPathResource pathResource = new ClassPathResource(String.format(JSON_PATH, userId, "groups"));
    if (!pathResource.exists()) {
        pathResource = new ClassPathResource(String.format(JSON_PATH, FALLBACK, "groups"));
    }
    try {
        return parser.parseGroups(pathResource.getInputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.surfnet.coin.api.service.MockServiceImpl.java

@Override
public Group20Entry getGroup20(String userId, String groupId, String onBehalfOf) {
    if (isActive()) {
        return getPreparedGroup20(groupId);
    }/*from  w w  w  .java  2 s  .  co m*/
    ClassPathResource pathResource = new ClassPathResource(String.format(JSON_PATH, groupId, "group20"));
    if (!pathResource.exists()) {
        pathResource = new ClassPathResource(String.format(JSON_PATH, FALLBACK, "group20"));
    }
    try {
        return parser.parseGroups20(pathResource.getInputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.surfnet.coin.api.service.MockServiceImpl.java

@Override
public Group20Entry getGroups20(String userId, String onBehalfOf, Integer count, Integer startIndex,
        String sortBy) {//  w  w w  . j  av a 2 s .  c  o m
    if (isActive()) {
        return getPreparedGroups20(userId);
    }
    ClassPathResource pathResource = new ClassPathResource(String.format(JSON_PATH, userId, "groups20"));
    if (!pathResource.exists()) {
        pathResource = new ClassPathResource(String.format(JSON_PATH, FALLBACK, "groups20"));
    }
    try {
        return parser.parseGroups20(pathResource.getInputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.surfnet.coin.api.service.MockServiceImpl.java

@Override
public GroupMembersEntry getGroupMembers(String groupId, String onBehalfOf, String spEntityId, Integer count,
        Integer startIndex, String sortBy) {
    if (isActive()) {
        return getPreparedGroupMembers(groupId);
    }//from   w  w  w . j  a v a2  s  .c  o m
    ClassPathResource pathResource = new ClassPathResource(String.format(JSON_PATH, groupId, "teammembers"));
    if (!pathResource.exists()) {
        pathResource = new ClassPathResource(String.format(JSON_PATH, FALLBACK, "teammembers"));
    }
    try {
        GroupMembersEntry entry = parser.parseTeamMembers(pathResource.getInputStream());
        processQueryOptions(entry, count, startIndex, sortBy, entry.getEntry());
        return entry;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ephesoft.dcma.batch.service.EphesoftContext.java

/**
 * Gets stream to read workflow properties for the server.
 * /*  w  w w .j ava 2 s  . c  o  m*/
 * @return {@link BufferedInputStream}
 * @throws IOException
 */
private BufferedInputStream getWorkflowPropertiesReaderStream() throws IOException {
    BufferedInputStream propertyInputStream = null;
    String filePath = BatchConstants.DCMA_WORKFLOWS_PROPERTIES;
    ClassPathResource classPathResourse = new ClassPathResource(filePath);
    if (classPathResourse.exists()) {
        propertyInputStream = new BufferedInputStream(classPathResourse.getInputStream());
    }
    return propertyInputStream;
}

From source file:org.surfnet.oaaas.auth.AuthorizationServerFilter.java

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    /*//from  w ww  .  j a v a  2  s  .  c  o  m
     * First check on the presence of a init-param where to look for the properties to support
     * multiple resource servers in the same war. Then look for second best apis-resource-server.properties file, then
     * try to use the filter config if parameters are present. If this also
     * fails trust on the setters (e.g. probably in test modus), but apply
     * fail-fast strategy
     */
    ClassPathResource res = null;
    String propertiesFile = filterConfig.getInitParameter("apis-resource-server.properties.file");
    if (StringUtils.isNotEmpty(propertiesFile)) {
        res = new ClassPathResource(propertiesFile);
    }
    if (res == null || !res.exists()) {
        res = new ClassPathResource("apis-resource-server.properties");
    }
    if (res != null && res.exists()) {
        Properties prop = new Properties();
        try {
            prop.load(res.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException("Error in reading the apis-resource-server.properties file", e);
        }
        resourceServerKey = prop.getProperty("adminService.resourceServerKey");
        resourceServerSecret = prop.getProperty("adminService.resourceServerSecret");
        authorizationServerUrl = prop.getProperty("adminService.tokenVerificationUrl");
        cacheEnabled = Boolean.valueOf(prop.getProperty("adminService.cacheEnabled"));
        String allowCorsRequestsProperty = prop.getProperty("adminService.allowCorsRequests");
        if (StringUtils.isNotEmpty(allowCorsRequestsProperty)) {
            allowCorsRequests = Boolean.valueOf(allowCorsRequestsProperty);
        }
        String typeInformationIsIncludedProperty = prop.getProperty("adminService.jsonTypeInfoIncluded");
        if (StringUtils.isNotEmpty(typeInformationIsIncludedProperty)) {
            typeInformationIsIncluded = Boolean.valueOf(typeInformationIsIncludedProperty);
        }
    } else if (filterConfig.getInitParameter("resource-server-key") != null) {
        resourceServerKey = filterConfig.getInitParameter("resource-server-key");
        resourceServerSecret = filterConfig.getInitParameter("resource-server-secret");
        authorizationServerUrl = filterConfig.getInitParameter("authorization-server-url");
        typeInformationIsIncluded = Boolean
                .valueOf(filterConfig.getInitParameter("type-information-is-included"));
    }
    Assert.hasText(resourceServerKey, "Must provide a resource server key");
    Assert.hasText(resourceServerSecret, "Must provide a resource server secret");
    Assert.hasText(authorizationServerUrl, "Must provide a authorization server url");

    this.authorizationValue = new String(
            Base64.encodeBase64(resourceServerKey.concat(":").concat(resourceServerSecret).getBytes()));
    if (cacheAccessTokens()) {
        this.cache = buildCache();
        Assert.notNull(this.cache);
    }

    this.client = createClient();

    this.objectMapper = createObjectMapper(typeInformationIsIncluded);
}