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;
}
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;
}
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);
}
}
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);
}
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;
}
Aggregations