1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.apphosting
.utils
.config
;
5 import java
.util
.ArrayList
;
8 import java
.util
.HashMap
;
15 private final List
<String
> servletPatterns
;
16 private final List
<SecurityConstraint
> securityConstraints
;
17 private final List
<String
> welcomeFiles
;
18 private final Map
<String
, String
> mimeMappings
;
19 private final Map
<String
, String
> patternToId
;
20 private boolean fallThroughToRuntime
;
23 servletPatterns
= new ArrayList
<String
>();
24 securityConstraints
= new ArrayList
<SecurityConstraint
>();
25 welcomeFiles
= new ArrayList
<String
>();
26 mimeMappings
= new HashMap
<String
, String
>();
27 patternToId
= new HashMap
<String
, String
>();
31 * Returns true if {@code url} matches one of the servlets or
32 * servlet filters listed in this web.xml.
34 public boolean matches(String url
) {
35 for (String pattern
: servletPatterns
) {
36 if (pattern
.length() == 0) {
39 if (pattern
.startsWith("*") && url
.endsWith(pattern
.substring(1))) {
41 } else if (pattern
.endsWith("*") &&
42 url
.startsWith(pattern
.substring(0, pattern
.length() - 1))) {
44 } else if (url
.equals(pattern
)) {
51 public String
getHandlerIdForPattern(String pattern
) {
52 return patternToId
.get(pattern
);
55 public void addServletPattern(String urlPattern
, String id
) {
56 YamlUtils
.validateUrl(urlPattern
);
57 servletPatterns
.add(urlPattern
);
59 patternToId
.put(urlPattern
, id
);
63 public List
<String
> getServletPatterns() {
64 return servletPatterns
;
67 public List
<SecurityConstraint
> getSecurityConstraints() {
68 return securityConstraints
;
71 public SecurityConstraint
addSecurityConstraint() {
72 SecurityConstraint context
= new SecurityConstraint();
73 securityConstraints
.add(context
);
77 public void addWelcomeFile(String welcomeFile
) {
78 welcomeFiles
.add(welcomeFile
);
81 public List
<String
> getWelcomeFiles() {
85 public void addMimeMapping(String extension
, String mimeType
) {
86 mimeMappings
.put(extension
, mimeType
);
89 public Map
<String
, String
> getMimeMappings() {
93 public String
getMimeTypeForPath(String path
) {
94 int dot
= path
.lastIndexOf(".");
96 return mimeMappings
.get(path
.substring(dot
+ 1));
102 public boolean getFallThroughToRuntime() {
103 return fallThroughToRuntime
;
106 public void setFallThroughToRuntime(boolean fallThroughToRuntime
) {
107 this.fallThroughToRuntime
= fallThroughToRuntime
;
111 * Performs some optional validation on this {@code WebXml}.
113 * @throws AppEngineConfigException If any errors are found.
115 public void validate() {
116 for (String welcomeFile
: welcomeFiles
) {
117 if (welcomeFile
.startsWith("/")) {
118 throw new AppEngineConfigException("Welcome files must be relative paths: " + welcomeFile
);
124 * Information about a security context, requiring SSL and/or authentication.
125 * Effectively, this is a tuple of { urlpatterns..., ssl-guarantee, auth-role }.
127 public static class SecurityConstraint
{
128 public enum RequiredRole
{ NONE
, ANY_USER
, ADMIN
}
129 public enum TransportGuarantee
{ NONE
, INTEGRAL
, CONFIDENTIAL
}
131 private final List
<String
> patterns
;
132 private TransportGuarantee transportGuarantee
= TransportGuarantee
.NONE
;
133 private RequiredRole requiredRole
= RequiredRole
.NONE
;
135 private SecurityConstraint() {
136 patterns
= new ArrayList
<String
>();
139 public List
<String
> getUrlPatterns() {
143 public void addUrlPattern(String pattern
) {
144 patterns
.add(pattern
);
147 public TransportGuarantee
getTransportGuarantee() {
148 return transportGuarantee
;
151 public void setTransportGuarantee(TransportGuarantee transportGuarantee
) {
152 this.transportGuarantee
= transportGuarantee
;
155 public RequiredRole
getRequiredRole() {
159 public void setRequiredRole(RequiredRole requiredRole
) {
160 this.requiredRole
= requiredRole
;