use of java.util.StringTokenizer in project jetty.project by eclipse.
the class HttpFields method valueParameters.
/**
* Get field value parameters. Some field values can have parameters. This method separates the
* value from the parameters and optionally populates a map with the parameters. For example:
*
* <PRE>
*
* FieldName : Value ; param1=val1 ; param2=val2
*
* </PRE>
*
* @param value The Field value, possibly with parameters.
* @param parameters A map to populate with the parameters, or null
* @return The value.
*/
public static String valueParameters(String value, Map<String, String> parameters) {
if (value == null)
return null;
int i = value.indexOf(';');
if (i < 0)
return value;
if (parameters == null)
return value.substring(0, i).trim();
StringTokenizer tok1 = new QuotedStringTokenizer(value.substring(i), ";", false, true);
while (tok1.hasMoreTokens()) {
String token = tok1.nextToken();
StringTokenizer tok2 = new QuotedStringTokenizer(token, "= ");
if (tok2.hasMoreTokens()) {
String paramName = tok2.nextToken();
String paramVal = null;
if (tok2.hasMoreTokens())
paramVal = tok2.nextToken();
parameters.put(paramName, paramVal);
}
}
return value.substring(0, i).trim();
}
use of java.util.StringTokenizer in project jetty.project by eclipse.
the class DefaultServlet method init.
/* ------------------------------------------------------------ */
@Override
public void init() throws UnavailableException {
_servletContext = getServletContext();
_contextHandler = initContextHandler(_servletContext);
_mimeTypes = _contextHandler.getMimeTypes();
_welcomes = _contextHandler.getWelcomeFiles();
if (_welcomes == null)
_welcomes = new String[] { "index.html", "index.jsp" };
_resourceService.setAcceptRanges(getInitBoolean("acceptRanges", _resourceService.isAcceptRanges()));
_resourceService.setDirAllowed(getInitBoolean("dirAllowed", _resourceService.isDirAllowed()));
_resourceService.setRedirectWelcome(getInitBoolean("redirectWelcome", _resourceService.isRedirectWelcome()));
_resourceService.setPrecompressedFormats(parsePrecompressedFormats(getInitParameter("precompressed"), getInitBoolean("gzip", false)));
_resourceService.setPathInfoOnly(getInitBoolean("pathInfoOnly", _resourceService.isPathInfoOnly()));
_resourceService.setEtags(getInitBoolean("etags", _resourceService.isEtags()));
if ("exact".equals(getInitParameter("welcomeServlets"))) {
_welcomeExactServlets = true;
_welcomeServlets = false;
} else
_welcomeServlets = getInitBoolean("welcomeServlets", _welcomeServlets);
_useFileMappedBuffer = getInitBoolean("useFileMappedBuffer", _useFileMappedBuffer);
_relativeResourceBase = getInitParameter("relativeResourceBase");
String rb = getInitParameter("resourceBase");
if (rb != null) {
if (_relativeResourceBase != null)
throw new UnavailableException("resourceBase & relativeResourceBase");
try {
_resourceBase = _contextHandler.newResource(rb);
} catch (Exception e) {
LOG.warn(Log.EXCEPTION, e);
throw new UnavailableException(e.toString());
}
}
String css = getInitParameter("stylesheet");
try {
if (css != null) {
_stylesheet = Resource.newResource(css);
if (!_stylesheet.exists()) {
LOG.warn("!" + css);
_stylesheet = null;
}
}
if (_stylesheet == null) {
_stylesheet = Resource.newResource(this.getClass().getResource("/jetty-dir.css"));
}
} catch (Exception e) {
LOG.warn(e.toString());
LOG.debug(e);
}
int encodingHeaderCacheSize = getInitInt("encodingHeaderCacheSize", -1);
if (encodingHeaderCacheSize >= 0)
_resourceService.setEncodingCacheSize(encodingHeaderCacheSize);
String cc = getInitParameter("cacheControl");
if (cc != null)
_resourceService.setCacheControl(new PreEncodedHttpField(HttpHeader.CACHE_CONTROL, cc));
String resourceCache = getInitParameter("resourceCache");
int max_cache_size = getInitInt("maxCacheSize", -2);
int max_cached_file_size = getInitInt("maxCachedFileSize", -2);
int max_cached_files = getInitInt("maxCachedFiles", -2);
if (resourceCache != null) {
if (max_cache_size != -1 || max_cached_file_size != -2 || max_cached_files != -2)
LOG.debug("ignoring resource cache configuration, using resourceCache attribute");
if (_relativeResourceBase != null || _resourceBase != null)
throw new UnavailableException("resourceCache specified with resource bases");
_cache = (CachedContentFactory) _servletContext.getAttribute(resourceCache);
}
try {
if (_cache == null && (max_cached_files != -2 || max_cache_size != -2 || max_cached_file_size != -2)) {
_cache = new CachedContentFactory(null, this, _mimeTypes, _useFileMappedBuffer, _resourceService.isEtags(), _resourceService.getPrecompressedFormats());
if (max_cache_size >= 0)
_cache.setMaxCacheSize(max_cache_size);
if (max_cached_file_size >= -1)
_cache.setMaxCachedFileSize(max_cached_file_size);
if (max_cached_files >= -1)
_cache.setMaxCachedFiles(max_cached_files);
_servletContext.setAttribute(resourceCache == null ? "resourceCache" : resourceCache, _cache);
}
} catch (Exception e) {
LOG.warn(Log.EXCEPTION, e);
throw new UnavailableException(e.toString());
}
HttpContent.ContentFactory contentFactory = _cache;
if (contentFactory == null) {
contentFactory = new ResourceContentFactory(this, _mimeTypes, _resourceService.getPrecompressedFormats());
if (resourceCache != null)
_servletContext.setAttribute(resourceCache, contentFactory);
}
_resourceService.setContentFactory(contentFactory);
_resourceService.setWelcomeFactory(this);
List<String> gzip_equivalent_file_extensions = new ArrayList<String>();
String otherGzipExtensions = getInitParameter("otherGzipFileExtensions");
if (otherGzipExtensions != null) {
//comma separated list
StringTokenizer tok = new StringTokenizer(otherGzipExtensions, ",", false);
while (tok.hasMoreTokens()) {
String s = tok.nextToken().trim();
gzip_equivalent_file_extensions.add((s.charAt(0) == '.' ? s : "." + s));
}
} else {
//.svgz files are gzipped svg files and must be served with Content-Encoding:gzip
gzip_equivalent_file_extensions.add(".svgz");
}
_resourceService.setGzipEquivalentFileExtensions(gzip_equivalent_file_extensions);
_servletHandler = _contextHandler.getChildHandlerByClass(ServletHandler.class);
for (ServletHolder h : _servletHandler.getServlets()) if (h.getServletInstance() == this)
_defaultHolder = h;
if (LOG.isDebugEnabled())
LOG.debug("resource base = " + _resourceBase);
}
use of java.util.StringTokenizer in project jetty.project by eclipse.
the class WebAppClassLoader method addClassPath.
/* ------------------------------------------------------------ */
/**
* @param classPath Comma or semicolon separated path of filenames or URLs
* pointing to directories or jar files. Directories should end
* with '/'.
* @throws IOException if unable to add classpath
*/
public void addClassPath(String classPath) throws IOException {
if (classPath == null)
return;
StringTokenizer tokenizer = new StringTokenizer(classPath, ",;");
while (tokenizer.hasMoreTokens()) {
Resource resource = _context.newResource(tokenizer.nextToken().trim());
if (LOG.isDebugEnabled())
LOG.debug("Path resource=" + resource);
// Add the resource
if (resource.isDirectory() && resource instanceof ResourceCollection)
addClassPath(resource);
else {
// Resolve file path if possible
File file = resource.getFile();
if (file != null) {
URL url = resource.getURI().toURL();
addURL(url);
} else if (resource.isDirectory()) {
addURL(resource.getURI().toURL());
} else {
if (LOG.isDebugEnabled())
LOG.debug("Check file exists and is not nested jar: " + resource);
throw new IllegalArgumentException("File not resolvable or incompatible with URLClassloader: " + resource);
}
}
}
}
use of java.util.StringTokenizer in project jetty.project by eclipse.
the class WebInfConfiguration method findExtraClasspathJars.
/**
* Get jars from WebAppContext.getExtraClasspath as resources
*
* @param context the context to find extra classpath jars in
* @return the list of Resources with the extra classpath, or null if not found
* @throws Exception if unable to find the extra classpath jars
*/
protected List<Resource> findExtraClasspathJars(WebAppContext context) throws Exception {
if (context == null || context.getExtraClasspath() == null)
return null;
List<Resource> jarResources = new ArrayList<Resource>();
StringTokenizer tokenizer = new StringTokenizer(context.getExtraClasspath(), ",;");
while (tokenizer.hasMoreTokens()) {
Resource resource = context.newResource(tokenizer.nextToken().trim());
String fnlc = resource.getName().toLowerCase(Locale.ENGLISH);
int dot = fnlc.lastIndexOf('.');
String extension = (dot < 0 ? null : fnlc.substring(dot));
if (extension != null && (extension.equals(".jar") || extension.equals(".zip"))) {
jarResources.add(resource);
}
}
return jarResources;
}
use of java.util.StringTokenizer in project jetty.project by eclipse.
the class AnnotationParser method parse.
protected void parse(Set<? extends Handler> handlers, Bundle bundle) throws Exception {
URI uri = _bundleToUri.get(bundle);
if (!_alreadyParsed.add(uri)) {
return;
}
String bundleClasspath = (String) bundle.getHeaders().get(Constants.BUNDLE_CLASSPATH);
if (bundleClasspath == null) {
bundleClasspath = ".";
}
//order the paths first by the number of tokens in the path second alphabetically.
TreeSet<String> paths = new TreeSet<String>(new Comparator<String>() {
public int compare(String o1, String o2) {
int paths1 = new StringTokenizer(o1, "/", false).countTokens();
int paths2 = new StringTokenizer(o2, "/", false).countTokens();
if (paths1 == paths2) {
return o1.compareTo(o2);
}
return paths2 - paths1;
}
});
boolean hasDotPath = false;
StringTokenizer tokenizer = new StringTokenizer(bundleClasspath, ",;", false);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken().trim();
if (!token.startsWith("/")) {
token = "/" + token;
}
if (token.equals("/.")) {
hasDotPath = true;
} else if (!token.endsWith(".jar") && !token.endsWith("/")) {
paths.add(token + "/");
} else {
paths.add(token);
}
}
//however it makes our life so much easier during development.
if (bundle.getEntry("/.classpath") != null) {
if (bundle.getEntry("/bin/") != null) {
paths.add("/bin/");
} else if (bundle.getEntry("/target/classes/") != null) {
paths.add("/target/classes/");
}
}
Enumeration classes = bundle.findEntries("/", "*.class", true);
if (classes == null) {
return;
}
while (classes.hasMoreElements()) {
URL classUrl = (URL) classes.nextElement();
String path = classUrl.getPath();
//remove the longest path possible:
String name = null;
for (String prefixPath : paths) {
if (path.startsWith(prefixPath)) {
name = path.substring(prefixPath.length());
break;
}
}
if (name == null && hasDotPath) {
//remove the starting '/'
name = path.substring(1);
}
if (name == null) {
//or the bundle classpath wasn't simply ".", so skip it
continue;
}
//transform into a classname to pass to the resolver
String shortName = name.replace('/', '.').substring(0, name.length() - 6);
if (!isParsed(shortName)) {
try (InputStream classInputStream = classUrl.openStream()) {
scanClass(handlers, getResource(bundle), classInputStream);
}
}
}
}
Aggregations