001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jk.config;
019:
020: import java.io.File;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.io.PrintWriter;
024: import java.util.Vector;
025:
026: import org.w3c.dom.Node;
027:
028: /* Naming conventions:
029:
030: JK_CONF_DIR == serverRoot/work ( XXX /jkConfig ? )
031:
032: - Each vhost has a sub-dir named after the canonycal name
033:
034: - For each webapp in a vhost, there is a separate WEBAPP_NAME.jkmap
035:
036: - In httpd.conf ( or equivalent servers ), in each virtual host you
037: should "Include JK_CONF_DIR/VHOST/jk_apache.conf". The config
038: file will contain the Alias declarations and other rules required
039: for apache operation. Same for other servers.
040:
041: - WebXml2Jk will be invoked by a config tool or automatically for each
042: webapp - it'll generate the WEBAPP.jkmap files and config fragments.
043:
044: WebXml2Jk will _not_ generate anything else but mappings.
045: It should _not_ try to guess locations or anything else - that's
046: another components' job.
047:
048: */
049:
050: /**
051: *
052: * @author Costin Manolache
053: */
054: public class GeneratorApache2 implements WebXml2Jk.MappingGenerator {
055: WebXml2Jk wxml;
056: String vhost;
057: String cpath;
058: String worker;
059: PrintWriter out;
060:
061: public void setWebXmlReader(WebXml2Jk wxml) {
062: this .wxml = wxml;
063: vhost = wxml.vhost;
064: cpath = wxml.cpath;
065: worker = wxml.worker;
066: }
067:
068: public void generateStart() throws IOException {
069: File base = wxml.getJkDir();
070: File outF = new File(base, "jk2.conf");
071: out = new PrintWriter(new FileWriter(outF));
072:
073: out.println("# Must be included in a virtual host context for "
074: + vhost);
075:
076: out.println("Alias " + cpath + " \"" + wxml.docBase + "\"");
077: out.println("<Directory \"" + wxml.docBase + "\" >");
078: out.println(" Options Indexes FollowSymLinks");
079: generateMimeMapping(out);
080: generateWelcomeFiles(out);
081:
082: // If we use this instead of extension mapping for jsp we can avoid most
083: // jsp security problems.
084: out.println(" AddHandler jakarta-servlet2 .jsp");
085: out.println("</Directory>");
086: out.println();
087:
088: out.println("<Location \"" + cpath + "/WEB-INF\" >");
089: out.println(" AllowOverride None");
090: out.println(" Deny from all");
091: out.println("</Location>");
092: out.println();
093: out.println("<Location \"" + cpath + "/META-INF\" >");
094: out.println(" AllowOverride None");
095: out.println(" Deny from all");
096: out.println("</Location>");
097: out.println();
098: }
099:
100: private void generateWelcomeFiles(PrintWriter out) {
101: Vector wf = wxml.getWellcomeFiles();
102: out.print(" DirectoryIndex ");
103: for (int i = 0; i < wf.size(); i++) {
104: out.print(" " + (String) wf.elementAt(i));
105: }
106: out.println();
107: }
108:
109: private void generateMimeMapping(PrintWriter out) {
110: Node webN = wxml.getWebXmlNode();
111: for (Node mapN = WebXml2Jk.getChild(webN, "mime-mapping"); mapN != null; mapN = WebXml2Jk
112: .getNext(mapN)) {
113: String ext = WebXml2Jk.getChildContent(mapN, "extension");
114: String type = WebXml2Jk.getChildContent(mapN, "mime-type");
115:
116: out.println(" AddType " + type + " " + ext);
117: }
118:
119: }
120:
121: public void generateEnd() {
122: out.close();
123: }
124:
125: public void generateServletMapping(String servlet, String url) {
126: out.println("<Location \"" + cpath + url + "\" >");
127: out.println(" SetHandler jakarta-servlet2");
128: out.println(" JkUriSet group " + worker);
129: out.println(" JkUriSet servlet " + servlet);
130: out.println(" JkUriSet host " + vhost);
131: out.println(" JkUriSet context " + cpath);
132: out.println("</Location>");
133: out.println();
134: }
135:
136: public void generateFilterMapping(String servlet, String url) {
137: out.println("<Location \"" + cpath + url + "\" >");
138: out.println(" SetHandler jakarta-servlet2");
139: out.println(" JkUriSet group " + worker);
140: out.println(" JkUriSet servlet " + servlet);
141: out.println(" JkUriSet host " + vhost);
142: out.println(" JkUriSet context " + cpath);
143: out.println("</Location>");
144: out.println();
145: }
146:
147: public void generateLoginConfig(String loginPage, String errPage,
148: String authM) {
149: out.println("<Location \"" + cpath + loginPage + "\" >");
150: out.println(" SetHandler jakarta-servlet2");
151: out.println(" JkUriSet group " + worker);
152: out.println(" JkUriSet host " + vhost);
153: out.println(" JkUriSet context " + cpath);
154: out.println("</Location>");
155: out.println();
156: }
157:
158: public void generateErrorPage(int err, String location) {
159:
160: }
161:
162: // XXX Only if BASIC/DIGEST and 'integrated auth'
163: public void generateConstraints(Vector urls, Vector methods,
164: Vector roles, boolean isSSL) {
165: for (int i = 0; i < urls.size(); i++) {
166: String url = (String) urls.elementAt(i);
167:
168: out.println("<Location \"" + cpath + url + "\" >");
169:
170: if (methods.size() > 0) {
171: out.print(" <Limit ");
172: for (int j = 0; j < methods.size(); j++) {
173: String m = (String) methods.elementAt(j);
174: out.print(" " + m);
175: }
176: out.println(" >");
177: }
178:
179: out.println(" AuthType basic");
180: out.print(" Require group ");
181: for (int j = 0; j < roles.size(); j++) {
182: String role = (String) roles.elementAt(j);
183: out.print(" " + role);
184: }
185: out.println();
186:
187: if (methods.size() > 0) {
188: out.println(" </Limit>");
189: }
190:
191: out.println("</Location>");
192: }
193: }
194: }
|