001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.server.rewrite;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.annotation.PostConstruct;
035:
036: import java.util.regex.Pattern;
037: import java.util.*;
038: import java.util.logging.*;
039:
040: import com.caucho.util.*;
041: import com.caucho.java.*;
042: import com.caucho.vfs.*;
043: import com.caucho.i18n.*;
044: import com.caucho.server.connection.*;
045:
046: /**
047: * A rewrite condition that passes if the value of a query parameter exists
048: * and matches a regular expression.
049: */
050: public class QueryParamCondition extends AbstractCondition {
051: private static final Logger log = Logger
052: .getLogger(QueryParamCondition.class.getName());
053:
054: private final String _param;
055: private Pattern _regexp;
056: private boolean _caseInsensitive;
057:
058: QueryParamCondition(String param) {
059: _param = param;
060: }
061:
062: public String getTagName() {
063: return "query-param";
064: }
065:
066: public void setRegexp(Pattern pattern) {
067: _regexp = pattern;
068: }
069:
070: public void setCaseInsensitive(boolean caseInsensitive) {
071: _caseInsensitive = caseInsensitive;
072: }
073:
074: @PostConstruct
075: public void init() {
076: if (_regexp != null && _caseInsensitive)
077: _regexp = Pattern.compile(_regexp.pattern(),
078: Pattern.CASE_INSENSITIVE);
079: }
080:
081: public boolean isMatch(HttpServletRequest request,
082: HttpServletResponse response) {
083: String query = request.getQueryString();
084:
085: if (query == null)
086: return false;
087:
088: String charEncoding = request.getCharacterEncoding();
089:
090: if (charEncoding == null)
091: charEncoding = CharacterEncoding.getLocalEncoding();
092:
093: String javaEncoding = Encoding.getJavaName(charEncoding);
094:
095: Form formParser = new Form();
096: HashMapImpl<String, String[]> form = new HashMapImpl<String, String[]>();
097:
098: try {
099: formParser
100: .parseQueryString(form, query, javaEncoding, true);
101: } catch (java.io.IOException e) {
102: log.log(Level.FINE, e.toString(), e);
103:
104: return false;
105: }
106:
107: String[] value = form.get(_param);
108:
109: if (value == null || value.length == 0)
110: return false;
111: else
112: return _regexp == null || _regexp.matcher(value[0]).find();
113: }
114: }
|