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