@Override public boolean exists() { return this.delegate.exists(); }
@Override public Resource getResource(String path) { Resource resource = super.getResource(path); if (resource == null) return null; if (!(resource instanceof PathResource) || !resource.exists()) return resource; try { File f = resource.getFile(); if (f.isDirectory() && !path.equals("/")) return resource; CacheResource cache = resourceCache.get(f); if (cache != null) return cache; if (f.length() < CACHE_LIMIT || f.getName().endsWith(".html") || path.equals("/")) { cache = new CacheResource((PathResource) resource); resourceCache.put(f, cache); return cache; } } catch (IOException e) { e.printStackTrace(); } return resource; }
/** * Get class dirs from WebAppContext.getExtraClasspath as resources * * @param context the context to look for extra classpaths in * @return the list of Resources to the extra classpath * @throws Exception if unable to find the extra classpaths */ protected List<Resource> findExtraClasspathDirs(WebAppContext context) throws Exception { if (context == null || context.getExtraClasspath() == null) return null; List<Resource> dirResources = new ArrayList<Resource>(); StringTokenizer tokenizer = new StringTokenizer(context.getExtraClasspath(), ",;"); while (tokenizer.hasMoreTokens()) { Resource resource = context.newResource(tokenizer.nextToken().trim()); if (resource.exists() && resource.isDirectory()) dirResources.add(resource); } return dirResources; }
private Server buildServerOrApplyConfiguration(String jettyConfiguration, Server server) throws Exception { // try local file first Resource jettyConfigurationFile = Resource.newResource(jettyConfiguration); if (jettyConfigurationFile == null || !jettyConfigurationFile.exists()) { // fallback to classpath jettyConfigurationFile = Resource.newClassPathResource(jettyConfiguration); if (jettyConfigurationFile == null || !jettyConfigurationFile.exists()) { throw new FileNotFoundException("Unable to find jetty configuration file either locally or on classpath '" + jettyConfiguration + "'"); } } logger.info("Applying jetty configuration '{}'", jettyConfigurationFile); try (InputStream is = jettyConfigurationFile.getInputStream()) { XmlConfiguration configuration = new XmlConfiguration(is); // create or apply to existing if (server == null) { return (Server)configuration.configure(); } else { return (Server)configuration.configure(server); } } }
@Override public Resource getResource(String uriInContext) throws MalformedURLException { if (uriInContext==null || !uriInContext.startsWith(URIUtil.SLASH)) throw new MalformedURLException(uriInContext); IOException ioe= null; Resource resource= null; int loop=0; while (uriInContext!=null && loop++<100) { try { resource= super.getResource(uriInContext); if (resource != null && resource.exists()) return resource; uriInContext = getResourceAlias(uriInContext); } catch (IOException e) { LOG.ignore(e); if (ioe==null) ioe= e; } } if (ioe != null && ioe instanceof MalformedURLException) throw (MalformedURLException)ioe; return resource; }
public Resource getWebInf() throws IOException { if (super.getBaseResource() == null) return null; // Iw there a WEB-INF directory? Resource web_inf= super.getBaseResource().addPath("WEB-INF/"); if (!web_inf.exists() || !web_inf.isDirectory()) return null; return web_inf; }
/** * Get <code>WEB-INF/classes</code> dir * * @param context the context to look for the <code>WEB-INF/classes</code> directory * @return the Resource for the <code>WEB-INF/classes</code> directory * @throws Exception if unable to find the <code>WEB-INF/classes</code> directory */ protected Resource findWebInfClassesDir (WebAppContext context) throws Exception { if (context == null) return null; Resource web_inf = context.getWebInf(); // Find WEB-INF/classes if (web_inf != null && web_inf.isDirectory()) { // Look for classes directory Resource classes= web_inf.addPath("classes/"); if (classes.exists()) return classes; } return null; }
@Override public URL getResource(String path) throws MalformedURLException { Resource resource=WebAppContext.this.getResource(path); if (resource==null || !resource.exists()) return null; // Should we go to the original war? if (resource.isDirectory() && resource instanceof ResourceCollection && !WebAppContext.this.isExtractWAR()) { Resource[] resources = ((ResourceCollection)resource).getResources(); for (int i=resources.length;i-->0;) { if (resources[i].getName().startsWith("jar:file")) return resources[i].getURI().toURL(); } } return resource.getURI().toURL(); }
private void configureDocumentRoot(WebAppContext handler) { File root = getValidDocumentRoot(); File docBase = (root != null) ? root : createTempDir("jetty-docbase"); try { List<Resource> resources = new ArrayList<>(); Resource rootResource = (docBase.isDirectory() ? Resource.newResource(docBase.getCanonicalFile()) : JarResource.newJarResource(Resource.newResource(docBase))); resources.add((root != null) ? new LoaderHidingResource(rootResource) : rootResource); for (URL resourceJarUrl : this.getUrlsOfJarsWithMetaInfResources()) { Resource resource = createResource(resourceJarUrl); if (resource.exists() && resource.isDirectory()) { resources.add(resource); } } handler.setBaseResource( new ResourceCollection(resources.toArray(new Resource[0]))); } catch (Exception ex) { throw new IllegalStateException(ex); } }
protected Resource findWebXml(WebAppContext context) throws IOException, MalformedURLException { String descriptor = context.getDescriptor(); if (descriptor != null) { Resource web = context.newResource(descriptor); if (web.exists() && !web.isDirectory()) return web; } Resource web_inf = context.getWebInf(); if (web_inf != null && web_inf.isDirectory()) { // do web.xml file Resource web = web_inf.addPath("web.xml"); if (web.exists()) return web; if (LOG.isDebugEnabled()) LOG.debug("No WEB-INF/web.xml in " + context.getWar() + ". Serving files and default/dynamic servlets only"); } return null; }
LOG.debug("Try webapp=" + web_app + ", exists=" + web_app.exists() + ", directory=" + web_app.isDirectory()+" file="+(web_app.getFile())); if (web_app.exists() && !web_app.isDirectory() && !web_app.toString().startsWith("jar:")) if (jarWebApp.exists() && jarWebApp.isDirectory()) web_app= jarWebApp; if (web_app.exists() && ( (context.isCopyWebDir() && web_app.getFile() != null && web_app.getFile().isDirectory()) || (context.isExtractWAR() && web_app.getFile() != null && !web_app.getFile().isDirectory()) || if (!web_app.exists() || !web_app.isDirectory()) webInfDir.mkdir(); if (web_inf_lib.exists()) if (web_inf_classes.exists())
if (lib.exists() && lib.isDirectory())
webFrag = Resource.newResource(uriJarPrefix(uri,"!/META-INF/web-fragment.xml")); if (!webFrag.exists() || webFrag.isDirectory())
if (!resourcesDir.exists() || !resourcesDir.isDirectory())
public static KeyStore getKeyStore(Resource store, String storeType, String storeProvider, String storePassword) throws Exception { KeyStore keystore = null; if (store != null) { Objects.requireNonNull(storeType, "storeType cannot be null"); if (storeProvider != null) { keystore = KeyStore.getInstance(storeType, storeProvider); } else { keystore = KeyStore.getInstance(storeType); } if (!store.exists()) throw new IllegalStateException("no valid keystore"); try (InputStream inStream = store.getInputStream()) { keystore.load(inStream, storePassword == null ? null : storePassword.toCharArray()); } } return keystore; }
@Override public Resource getResource(String path) { try { Resource resource = super.getResource(path); if (resource == null) return null; if (!(resource instanceof PathResource) || !resource.exists()) return resource; File f = resource.getFile(); if (f.isDirectory() && !path.equals("/")) return resource; CacheResource cache = resourceCache.get(f); if (cache != null) return cache; if (f.length() < CACHE_LIMIT || f.getName().endsWith(".html") || path.equals("/")) { cache = new CacheResource((PathResource) resource); resourceCache.put(f, cache); return cache; } return resource; } catch (IOException e) { Data.logger.warn("", e); } return null; }
/** * Instantiates a new resource collection. * * @param resources the resources to be added to collection */ public ResourceCollection(Resource... resources) { List<Resource> list = new ArrayList<Resource>(); for (Resource r : resources) { if (r==null) continue; if (r instanceof ResourceCollection) { for (Resource r2 : ((ResourceCollection)r).getResources()) list.add(r2); } else list.add(r); } _resources = list.toArray(new Resource[list.size()]); for(Resource r : _resources) { if(!r.exists() || !r.isDirectory()) throw new IllegalArgumentException(r + " is not an existing directory."); } }