Search in sources :

Example 1 with Resource

use of org.onebusaway.presentation.services.resources.Resource in project onebusaway-application-modules by camsys.

the class ResourceServiceImpl method getResourceForPaths.

/**
 **
 * Multi-Resource Methods
 ***
 */
private Resource getResourceForPaths(String resourceId, List<String> resourcePaths, LocaleProvider localeProvider) {
    if (resourceId == null) {
        resourceId = getResourceIdForResourcePaths(resourcePaths);
    } else {
        _resourcePathsById.putIfAbsent(resourceId, resourcePaths);
    }
    String resourceIdKey = getResourcePathAsKey(resourceId, localeProvider);
    Resource resource = _resourceEntriesByResourcePath.get(resourceIdKey);
    if (resource == null) {
        resource = createResourceForPaths(resourceId, resourcePaths, localeProvider);
        if (resource == null)
            return null;
        Resource existingResource = _resourceEntriesByResourcePath.putIfAbsent(resourceIdKey, resource);
        if (existingResource != null)
            return existingResource;
    }
    return resource;
}
Also used : Resource(org.onebusaway.presentation.services.resources.Resource)

Example 2 with Resource

use of org.onebusaway.presentation.services.resources.Resource in project onebusaway-application-modules by camsys.

the class ResourceServiceImpl method createResourceForPaths.

private Resource createResourceForPaths(String resourceId, List<String> resourcePaths, LocaleProvider localeProvider) {
    List<Resource> resources = new ArrayList<Resource>(resourcePaths.size());
    String extension = null;
    for (String resourcePath : resourcePaths) {
        Resource resource = getResourceForPath(resourcePath, localeProvider, null);
        if (resource == null)
            return null;
        resources.add(resource);
        if (extension == null) {
            int index = resourcePath.lastIndexOf('.');
            if (index != -1)
                extension = resourcePath.substring(index + 1);
        }
    }
    if (extension != null)
        resourceId += "." + extension;
    ResourcesEntry entry = new ResourcesEntry(resourceId, resources);
    generateLocalResourcesAndExternalUrl(entry);
    return entry;
}
Also used : Resource(org.onebusaway.presentation.services.resources.Resource) ArrayList(java.util.ArrayList)

Example 3 with Resource

use of org.onebusaway.presentation.services.resources.Resource in project onebusaway-application-modules by camsys.

the class ResourceServiceImpl method getCollectionResourceAsSourceUrl.

private URL getCollectionResourceAsSourceUrl(String resourceName, LocaleProvider localeProvider) {
    int index = resourceName.indexOf('=');
    if (index == -1)
        throw new IllegalStateException("invalid resource collection specifier: " + resourceName);
    String collectionPrefix = resourceName.substring(0, index);
    String collectionResourcePath = resourceName.substring(index + 1);
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Map<String, String> resourceMapping = new HashMap<String, String>();
    try {
        org.springframework.core.io.Resource[] resources = resolver.getResources(collectionResourcePath);
        for (org.springframework.core.io.Resource resource : resources) {
            URL url = resource.getURL();
            String name = getLocalUrlAsResourceName(url);
            Resource r = getResourceForPath(name, localeProvider, url);
            if (r != null) {
                String path = url.getPath();
                int sepIndex = path.lastIndexOf(File.separator);
                if (sepIndex != -1)
                    path = path.substring(sepIndex + 1);
                resourceMapping.put(path, r.getExternalUrl());
                String alternateId = PREFIX_COLLECTION_ENTRY + collectionPrefix + ":" + path;
                _resourceEntriesByResourcePath.put(alternateId, r);
            }
        }
        File file = getOutputFile(PREFIX_COLLECTION + collectionPrefix + ".js");
        PrintWriter out = new PrintWriter(file);
        JSONObject obj = new JSONObject(resourceMapping);
        out.println("var OBA = window.OBA || {};");
        out.println("if(!OBA.Resources) { OBA.Resources = {}; }");
        out.println("OBA.Resources." + collectionPrefix + " = " + obj.toString() + ";");
        out.close();
        return getFileAsUrl(file);
    } catch (IOException ex) {
        throw new IllegalStateException("error loading resources", ex);
    }
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Resource(org.onebusaway.presentation.services.resources.Resource) IOException(java.io.IOException) URL(java.net.URL) JSONObject(org.json.JSONObject) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 4 with Resource

use of org.onebusaway.presentation.services.resources.Resource in project onebusaway-application-modules by camsys.

the class ResourceServiceImpl method generateLocalResourcesAndExternalUrl.

private void generateLocalResourcesAndExternalUrl(ResourcesEntry entry) {
    File outputFile = getOutputFile(entry.getResourceId());
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
        for (Resource resource : entry.getResources()) {
            synchronized (resource) {
                URL url = resource.getLocalUrl();
                BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    out.write(line);
                    out.write('\n');
                }
                reader.close();
            }
        }
        out.close();
    } catch (IOException ex) {
        throw new IllegalStateException("error constructing resource", ex);
    }
    URL localUrl = getFileAsUrl(outputFile);
    entry.setLocalUrl(localUrl);
    entry.setContentLength(outputFile.length());
    String key = getResourceKey(localUrl);
    String externalId = constructExternalId(entry.getResourceId(), key);
    String externalUrl = constructExternalUrl(externalId);
    entry.setExternalId(externalId);
    entry.setExternalUrl(externalUrl);
    _resourceEntriesByExternalId.put(externalId, entry);
}
Also used : InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) Resource(org.onebusaway.presentation.services.resources.Resource) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter)

Example 5 with Resource

use of org.onebusaway.presentation.services.resources.Resource in project onebusaway-application-modules by camsys.

the class ResourceServiceImpl method getLocalResourceForExternalId.

@Override
public Resource getLocalResourceForExternalId(String externalId, Locale locale) {
    Resource resource = _resourceEntriesByExternalId.get(externalId);
    if (resource == null) {
        /**
         * In case the resource has not been first requested as a resource(url)
         * first
         */
        String resourcePath = getExternalIdAsResourcePath(externalId);
        if (resourcePath != null) {
            LocaleProvider localeProvider = new LocaleProviderImpl(locale);
            /**
             * First we see if this is a resource identified by id
             */
            if (_resourcePathsById.containsKey(resourcePath)) {
                List<String> paths = _resourcePathsById.get(resourcePath);
                resource = getResourceForPaths(resourcePath, paths, localeProvider);
            }
            if (resource == null)
                resource = getResourceForPath(resourcePath, localeProvider, null);
        }
    }
    if (resource == null) {
        _log.warn("resource not found for external id: " + externalId);
        return null;
    }
    return resource;
}
Also used : LocaleProvider(com.opensymphony.xwork2.LocaleProvider) Resource(org.onebusaway.presentation.services.resources.Resource)

Aggregations

Resource (org.onebusaway.presentation.services.resources.Resource)8 LocaleProvider (com.opensymphony.xwork2.LocaleProvider)3 File (java.io.File)2 IOException (java.io.IOException)2 URL (java.net.URL)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JSONObject (org.json.JSONObject)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)1