jetty-all-8.1.7.v20120910

16:37:28.438 INFO  jd.cli.Main - Decompiling jetty-all-8.1.7.v20120910.jar
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class RegexRule
  extends Rule
{
  protected Pattern _regex;
  
  public void setRegex(String regex)
  {
    _regex = Pattern.compile(regex);
  }
  
  public String getRegex()
  {
    return _regex == null ? null : _regex.pattern();
  }
  
  public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    Matcher matcher = _regex.matcher(target);
    boolean matches = matcher.matches();
    if (matches) {
      return apply(target, request, response, matcher);
    }
    return null;
  }
  
  protected abstract String apply(String paramString, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, Matcher paramMatcher)
    throws IOException;
  
  public String toString()
  {
    return super.toString() + "[" + _regex + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.RegexRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.util.URIUtil;

public class LegacyRule
  extends Rule
{
  private PathMap _rewrite = new PathMap(true);
  
  public LegacyRule()
  {
    _handling = false;
    _terminating = false;
  }
  
  public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    Map.Entry<?, ?> rewrite = _rewrite.getMatch(target);
    if ((rewrite != null) && (rewrite.getValue() != null))
    {
      target = URIUtil.addPaths(rewrite.getValue().toString(), PathMap.pathInfo(rewrite.getKey().toString(), target));
      
      return target;
    }
    return null;
  }
  
  public PathMap getRewrite()
  {
    return _rewrite;
  }
  
  public void setRewrite(PathMap rewrite)
  {
    _rewrite = rewrite;
  }
  
  public void addRewriteRule(String pattern, String prefix)
  {
    if ((pattern == null) || (pattern.length() == 0) || (!pattern.startsWith("/"))) {
      throw new IllegalArgumentException();
    }
    if (_rewrite == null) {
      _rewrite = new PathMap(true);
    }
    _rewrite.put(pattern, prefix);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.LegacyRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import org.eclipse.jetty.server.Request;

public abstract interface Rule$ApplyURI
{
  public abstract void applyURI(Request paramRequest, String paramString1, String paramString2)
    throws IOException;
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.Rule.ApplyURI
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponsePatternRule
  extends PatternRule
{
  private String _code;
  private String _reason = "";
  
  public ResponsePatternRule()
  {
    _handling = true;
    _terminating = true;
  }
  
  public void setCode(String code)
  {
    _code = code;
  }
  
  public void setReason(String reason)
  {
    _reason = reason;
  }
  
  public String apply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    int code = Integer.parseInt(_code);
    if (code >= 400) {
      response.sendError(code, _reason);
    } else {
      response.setStatus(code);
    }
    return target;
  }
  
  public String toString()
  {
    return super.toString() + "[" + _code + "," + _reason + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.ResponsePatternRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HeaderPatternRule
  extends PatternRule
{
  private String _name;
  private String _value;
  private boolean _add = false;
  
  public HeaderPatternRule()
  {
    _handling = false;
    _terminating = false;
  }
  
  public void setName(String name)
  {
    _name = name;
  }
  
  public void setValue(String value)
  {
    _value = value;
  }
  
  public void setAdd(boolean add)
  {
    _add = add;
  }
  
  public String apply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    if (_add) {
      response.addHeader(_name, _value);
    } else {
      response.setHeader(_name, _value);
    }
    return target;
  }
  
  public String getName()
  {
    return _name;
  }
  
  public String getValue()
  {
    return _value;
  }
  
  public boolean isAdd()
  {
    return _add;
  }
  
  public String toString()
  {
    return super.toString() + "[" + _name + "," + _value + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.HeaderPatternRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.LazyList;

public class VirtualHostRuleContainer
  extends RuleContainer
{
  private String[] _virtualHosts;
  
  public void setVirtualHosts(String[] virtualHosts)
  {
    if (virtualHosts == null)
    {
      _virtualHosts = virtualHosts;
    }
    else
    {
      _virtualHosts = new String[virtualHosts.length];
      for (int i = 0; i < virtualHosts.length; i++) {
        _virtualHosts[i] = normalizeHostname(virtualHosts[i]);
      }
    }
  }
  
  public String[] getVirtualHosts()
  {
    return _virtualHosts;
  }
  
  public void addVirtualHost(String virtualHost)
  {
    _virtualHosts = ((String[])LazyList.addToArray(_virtualHosts, virtualHost, String.class));
  }
  
  public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    if ((_virtualHosts != null) && (_virtualHosts.length > 0))
    {
      String requestHost = normalizeHostname(request.getServerName());
      for (String ruleHost : _virtualHosts) {
        if ((ruleHost == null) || (ruleHost.equalsIgnoreCase(requestHost)) || ((ruleHost.startsWith("*.")) && (ruleHost.regionMatches(true, 2, requestHost, requestHost.indexOf(".") + 1, ruleHost.length() - 2)))) {
          return apply(target, request, response);
        }
      }
    }
    else
    {
      return apply(target, request, response);
    }
    return null;
  }
  
  private String normalizeHostname(String host)
  {
    if (host == null) {
      return null;
    }
    if (host.endsWith(".")) {
      return host.substring(0, host.length() - 1);
    }
    return host;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.VirtualHostRuleContainer
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookiePatternRule
  extends PatternRule
{
  private String _name;
  private String _value;
  
  public CookiePatternRule()
  {
    _handling = false;
    _terminating = false;
  }
  
  public void setName(String name)
  {
    _name = name;
  }
  
  public void setValue(String value)
  {
    _value = value;
  }
  
  public String apply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    response.addCookie(new Cookie(_name, _value));
    return target;
  }
  
  public String toString()
  {
    return super.toString() + "[" + _name + "," + _value + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.CookiePatternRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class HeaderRule
  extends Rule
{
  private String _header;
  private String _headerValue;
  
  public String getHeader()
  {
    return _header;
  }
  
  public void setHeader(String header)
  {
    _header = header;
  }
  
  public String getHeaderValue()
  {
    return _headerValue;
  }
  
  public void setHeaderValue(String headerValue)
  {
    _headerValue = headerValue;
  }
  
  public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    String requestHeaderValue = request.getHeader(_header);
    if ((requestHeaderValue != null) && (
      (_headerValue == null) || (_headerValue.equals(requestHeaderValue)))) {
      apply(target, requestHeaderValue, request, response);
    }
    return null;
  }
  
  protected abstract String apply(String paramString1, String paramString2, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws IOException;
  
  public String toString()
  {
    return super.toString() + "[" + _header + ":" + _headerValue + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.HeaderRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;

public abstract class Rule
{
  protected boolean _terminating;
  protected boolean _handling;
  
  public abstract String matchAndApply(String paramString, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws IOException;
  
  public void setTerminating(boolean terminating)
  {
    _terminating = terminating;
  }
  
  public boolean isTerminating()
  {
    return _terminating;
  }
  
  public boolean isHandling()
  {
    return _handling;
  }
  
  public void setHandling(boolean handling)
  {
    _handling = handling;
  }
  
  public String toString()
  {
    return getClass().getName() + (_handling ? "[H" : "[h") + (_terminating ? "T]" : "t]");
  }
  
  public static abstract interface ApplyURI
  {
    public abstract void applyURI(Request paramRequest, String paramString1, String paramString2)
      throws IOException;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.Rule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;

public class RewriteRegexRule
  extends RegexRule
  implements Rule.ApplyURI
{
  private String _replacement;
  private String _query;
  private boolean _queryGroup;
  
  public RewriteRegexRule()
  {
    _handling = false;
    _terminating = false;
  }
  
  public void setReplacement(String replacement)
  {
    String[] split = replacement.split("\\?", 2);
    _replacement = split[0];
    _query = (split.length == 2 ? split[1] : null);
    _queryGroup = ((_query != null) && (_query.contains("$Q")));
  }
  
  public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher)
    throws IOException
  {
    target = _replacement;
    String query = _query;
    for (int g = 1; g <= matcher.groupCount(); g++)
    {
      String group = matcher.group(g);
      if (group == null) {
        group = "";
      } else {
        group = Matcher.quoteReplacement(group);
      }
      target = target.replaceAll("\\$" + g, group);
      if (query != null) {
        query = query.replaceAll("\\$" + g, group);
      }
    }
    if (query != null)
    {
      if (_queryGroup) {
        query = query.replace("$Q", request.getQueryString() == null ? "" : request.getQueryString());
      }
      request.setAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q", query);
    }
    return target;
  }
  
  public void applyURI(Request request, String oldTarget, String newTarget)
    throws IOException
  {
    if (_query == null)
    {
      request.setRequestURI(newTarget);
    }
    else
    {
      String query = (String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
      if ((!_queryGroup) && (request.getQueryString() != null)) {
        query = request.getQueryString() + "&" + query;
      }
      HttpURI uri = new HttpURI(newTarget + "?" + query);
      request.setUri(uri);
      request.setRequestURI(newTarget);
      request.setQueryString(query);
    }
  }
  
  public String toString()
  {
    return super.toString() + "[" + _replacement + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.RewriteRegexRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.URIUtil;

public class RewritePatternRule
  extends PatternRule
  implements Rule.ApplyURI
{
  private String _replacement;
  
  public RewritePatternRule()
  {
    _handling = false;
    _terminating = false;
  }
  
  public void setReplacement(String value)
  {
    _replacement = value;
  }
  
  public String apply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    target = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern, target));
    return target;
  }
  
  public void applyURI(Request request, String oldTarget, String newTarget)
    throws IOException
  {
    String uri = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern, request.getRequestURI()));
    request.setRequestURI(uri);
  }
  
  public String toString()
  {
    return super.toString() + "[" + _replacement + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.RewritePatternRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RedirectPatternRule
  extends PatternRule
{
  private String _location;
  
  public RedirectPatternRule()
  {
    _handling = true;
    _terminating = true;
  }
  
  public void setLocation(String value)
  {
    _location = value;
  }
  
  public String apply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    response.sendRedirect(response.encodeRedirectURL(_location));
    return target;
  }
  
  public String toString()
  {
    return super.toString() + "[" + _location + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.RedirectPatternRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.StringMap;

public class MsieSslRule
  extends Rule
{
  private static final int IEv5 = 53;
  private static final int IEv6 = 54;
  private static StringMap __IE6_BadOS = new StringMap();
  
  public MsieSslRule()
  {
    __IE6_BadOS.put("NT 5.01", Boolean.TRUE);
    __IE6_BadOS.put("NT 5.0", Boolean.TRUE);
    __IE6_BadOS.put("NT 4.0", Boolean.TRUE);
    __IE6_BadOS.put("98", Boolean.TRUE);
    __IE6_BadOS.put("98; Win 9x 4.90", Boolean.TRUE);
    __IE6_BadOS.put("95", Boolean.TRUE);
    __IE6_BadOS.put("CE", Boolean.TRUE);
    
    _handling = false;
    _terminating = false;
  }
  
  public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    if (request.isSecure())
    {
      String user_agent = request.getHeader("User-Agent");
      if (user_agent != null)
      {
        int msie = user_agent.indexOf("MSIE");
        if ((msie > 0) && (user_agent.length() - msie > 5))
        {
          int ieVersion = user_agent.charAt(msie + 5);
          if (ieVersion <= 53)
          {
            response.setHeader("Connection", "close");
            return target;
          }
          if (ieVersion == 54)
          {
            int windows = user_agent.indexOf("Windows", msie + 5);
            if (windows > 0)
            {
              int end = user_agent.indexOf(')', windows + 8);
              if ((end < 0) || (__IE6_BadOS.getEntry(user_agent, windows + 8, end - windows - 8) != null))
              {
                response.setHeader("Connection", "close");
                return target;
              }
            }
          }
        }
      }
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.MsieSslRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class RuleContainer
  extends Rule
{
  private static final Logger LOG = Log.getLogger(RuleContainer.class);
  protected Rule[] _rules;
  protected String _originalPathAttribute;
  protected boolean _rewriteRequestURI = true;
  protected boolean _rewritePathInfo = true;
  protected LegacyRule _legacy;
  
  @Deprecated
  public LegacyRule getLegacyRule()
  {
    if (_legacy == null)
    {
      _legacy = new LegacyRule();
      addRule(_legacy);
    }
    return _legacy;
  }
  
  @Deprecated
  public void setLegacyRule(LegacyRule legacyRule)
  {
    _legacy = legacyRule;
  }
  
  public Rule[] getRules()
  {
    return _rules;
  }
  
  public void setRules(Rule[] rules)
  {
    if (_legacy == null)
    {
      _rules = rules;
    }
    else
    {
      _rules = null;
      addRule(_legacy);
      if (rules != null) {
        for (Rule rule : rules) {
          addRule(rule);
        }
      }
    }
  }
  
  public void addRule(Rule rule)
  {
    _rules = ((Rule[])LazyList.addToArray(_rules, rule, Rule.class));
  }
  
  public boolean isRewriteRequestURI()
  {
    return _rewriteRequestURI;
  }
  
  public void setRewriteRequestURI(boolean rewriteRequestURI)
  {
    _rewriteRequestURI = rewriteRequestURI;
  }
  
  public boolean isRewritePathInfo()
  {
    return _rewritePathInfo;
  }
  
  public void setRewritePathInfo(boolean rewritePathInfo)
  {
    _rewritePathInfo = rewritePathInfo;
  }
  
  public String getOriginalPathAttribute()
  {
    return _originalPathAttribute;
  }
  
  public void setOriginalPathAttribute(String originalPathAttribte)
  {
    _originalPathAttribute = originalPathAttribte;
  }
  
  public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    return apply(target, request, response);
  }
  
  protected String apply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    boolean original_set = _originalPathAttribute == null;
    for (Rule rule : _rules)
    {
      String applied = rule.matchAndApply(target, request, response);
      if (applied != null)
      {
        LOG.debug("applied {}", new Object[] { rule });
        LOG.debug("rewrote {} to {}", new Object[] { target, applied });
        if (!original_set)
        {
          original_set = true;
          request.setAttribute(_originalPathAttribute, target);
        }
        if (_rewriteRequestURI) {
          if ((rule instanceof Rule.ApplyURI)) {
            ((Rule.ApplyURI)rule).applyURI((Request)request, target, applied);
          } else {
            ((Request)request).setRequestURI(applied);
          }
        }
        if (_rewritePathInfo) {
          ((Request)request).setPathInfo(applied);
        }
        target = applied;
        if (rule.isHandling())
        {
          LOG.debug("handling {}", new Object[] { rule });
          ((request instanceof Request) ? (Request)request : AbstractHttpConnection.getCurrentConnection().getRequest()).setHandled(true);
        }
        if (rule.isTerminating())
        {
          LOG.debug("terminating {}", new Object[] { rule });
          break;
        }
      }
    }
    return target;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.RuleContainer
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;

public class ForwardedSchemeHeaderRule
  extends HeaderRule
{
  private String _scheme = "https";
  
  public String getScheme()
  {
    return _scheme;
  }
  
  public void setScheme(String scheme)
  {
    _scheme = scheme;
  }
  
  protected String apply(String target, String value, HttpServletRequest request, HttpServletResponse response)
  {
    ((Request)request).setScheme(_scheme);
    return target;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.ForwardedSchemeHeaderRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.http.HttpHeaderValues;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.util.log.Logger;

class ProxyRule$1
  extends HttpExchange
{
  ProxyRule$1(ProxyRule paramProxyRule, int paramInt, OutputStream paramOutputStream, HttpServletResponse paramHttpServletResponse) {}
  
  protected void onRequestCommitted()
    throws IOException
  {}
  
  protected void onRequestComplete()
    throws IOException
  {}
  
  protected void onResponseComplete()
    throws IOException
  {
    if (val$debug != 0) {
      ProxyRule.access$000().debug(val$debug + " complete", new Object[0]);
    }
  }
  
  protected void onResponseContent(Buffer content)
    throws IOException
  {
    if (val$debug != 0) {
      ProxyRule.access$000().debug(val$debug + " content" + content.length(), new Object[0]);
    }
    content.writeTo(val$out);
  }
  
  protected void onResponseHeaderComplete()
    throws IOException
  {}
  
  protected void onResponseStatus(Buffer version, int status, Buffer reason)
    throws IOException
  {
    if (val$debug != 0) {
      ProxyRule.access$000().debug(val$debug + " " + version + " " + status + " " + reason, new Object[0]);
    }
    if ((reason != null) && (reason.length() > 0)) {
      val$response.setStatus(status, reason.toString());
    } else {
      val$response.setStatus(status);
    }
  }
  
  protected void onResponseHeader(Buffer name, Buffer value)
    throws IOException
  {
    String s = name.toString().toLowerCase();
    if ((!ProxyRule.access$100(this$0).contains(s)) || ((HttpHeaders.CONNECTION_BUFFER.equals(name)) && (HttpHeaderValues.CLOSE_BUFFER.equals(value))))
    {
      if (val$debug != 0) {
        ProxyRule.access$000().debug(val$debug + " " + name + ": " + value, new Object[0]);
      }
      val$response.addHeader(name.toString(), value.toString());
    }
    else if (val$debug != 0)
    {
      ProxyRule.access$000().debug(val$debug + " " + name + "! " + value, new Object[0]);
    }
  }
  
  protected void onConnectionFailed(Throwable ex)
  {
    ProxyRule.access$000().warn(ex.toString(), new Object[0]);
    ProxyRule.access$000().debug(ex);
    if (!val$response.isCommitted()) {
      val$response.setStatus(500);
    }
  }
  
  protected void onException(Throwable ex)
  {
    if ((ex instanceof EofException))
    {
      ProxyRule.access$000().ignore(ex);
      return;
    }
    ProxyRule.access$000().warn(ex.toString(), new Object[0]);
    ProxyRule.access$000().debug(ex);
    if (!val$response.isCommitted()) {
      val$response.setStatus(500);
    }
  }
  
  protected void onExpire()
  {
    if (!val$response.isCommitted()) {
      val$response.setStatus(504);
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.ProxyRule.1
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.PathMap;

public abstract class PatternRule
  extends Rule
{
  protected String _pattern;
  
  public String getPattern()
  {
    return _pattern;
  }
  
  public void setPattern(String pattern)
  {
    _pattern = pattern;
  }
  
  public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    if (PathMap.match(_pattern, target)) {
      return apply(target, request, response);
    }
    return null;
  }
  
  protected abstract String apply(String paramString, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws IOException;
  
  public String toString()
  {
    return super.toString() + "[" + _pattern + "]";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.PatternRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.HandlerWrapper;

public class RewriteHandler
  extends HandlerWrapper
{
  private RuleContainer _rules;
  
  public RewriteHandler()
  {
    _rules = new RuleContainer();
  }
  
  @Deprecated
  public void setLegacyRule(LegacyRule legacyRule)
  {
    _rules.setLegacyRule(legacyRule);
  }
  
  public Rule[] getRules()
  {
    return _rules.getRules();
  }
  
  public void setRules(Rule[] rules)
  {
    _rules.setRules(rules);
  }
  
  public void setRules(RuleContainer rules)
  {
    _rules = rules;
  }
  
  public void addRule(Rule rule)
  {
    _rules.addRule(rule);
  }
  
  public boolean isRewriteRequestURI()
  {
    return _rules.isRewriteRequestURI();
  }
  
  public void setRewriteRequestURI(boolean rewriteRequestURI)
  {
    _rules.setRewriteRequestURI(rewriteRequestURI);
  }
  
  public boolean isRewritePathInfo()
  {
    return _rules.isRewritePathInfo();
  }
  
  public void setRewritePathInfo(boolean rewritePathInfo)
  {
    _rules.setRewritePathInfo(rewritePathInfo);
  }
  
  public String getOriginalPathAttribute()
  {
    return _rules.getOriginalPathAttribute();
  }
  
  public void setOriginalPathAttribute(String originalPathAttribute)
  {
    _rules.setOriginalPathAttribute(originalPathAttribute);
  }
  
  public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    if (isStarted())
    {
      String returned = _rules.matchAndApply(target, request, response);
      target = returned == null ? target : returned;
      if (!baseRequest.isHandled()) {
        super.handle(target, baseRequest, request, response);
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.RewriteHandler
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RedirectRegexRule
  extends RegexRule
{
  private String _replacement;
  
  public RedirectRegexRule()
  {
    _handling = true;
    _terminating = true;
  }
  
  public void setReplacement(String replacement)
  {
    _replacement = replacement;
  }
  
  protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher)
    throws IOException
  {
    target = _replacement;
    for (int g = 1; g <= matcher.groupCount(); g++)
    {
      String group = matcher.group(g);
      target = target.replaceAll("\\$" + g, group);
    }
    response.sendRedirect(response.encodeRedirectURL(target));
    return target;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.rewrite.handler.RedirectRegexRule
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.rewrite.handler;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.HashSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.http.HttpHeaderValues;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

public class ProxyRule
  extends PatternRule
{
  private static final Logger _log = Log.getLogger(ProxyRule.class);
  private HttpClient _client;
  private String _hostHeader;
  private String _proxyTo;
  private int _connectorType = 2;
  private String _maxThreads;
  private String _maxConnections;
  private String _timeout;
  private String _idleTimeout;
  private String _requestHeaderSize;
  private String _requestBufferSize;
  private String _responseHeaderSize;
  private String _responseBufferSize;
  private HashSet<String> _DontProxyHeaders = new HashSet();
  
  public ProxyRule()
  {
    _DontProxyHeaders.add("proxy-connection");
    _DontProxyHeaders.add("connection");
    _DontProxyHeaders.add("keep-alive");
    _DontProxyHeaders.add("transfer-encoding");
    _DontProxyHeaders.add("te");
    _DontProxyHeaders.add("trailer");
    _DontProxyHeaders.add("proxy-authorization");
    _DontProxyHeaders.add("proxy-authenticate");
    _DontProxyHeaders.add("upgrade");
    
    _handling = true;
    _terminating = true;
  }
  
  private void initializeClient()
    throws Exception
  {
    _client = new HttpClient();
    _client.setConnectorType(_connectorType);
    if (_maxThreads != null) {
      _client.setThreadPool(new QueuedThreadPool(Integer.parseInt(_maxThreads)));
    } else {
      _client.setThreadPool(new QueuedThreadPool());
    }
    if (_maxConnections != null) {
      _client.setMaxConnectionsPerAddress(Integer.parseInt(_maxConnections));
    }
    if (_timeout != null) {
      _client.setTimeout(Long.parseLong(_timeout));
    }
    if (_idleTimeout != null) {
      _client.setIdleTimeout(Long.parseLong(_idleTimeout));
    }
    if (_requestBufferSize != null) {
      _client.setRequestBufferSize(Integer.parseInt(_requestBufferSize));
    }
    if (_requestHeaderSize != null) {
      _client.setRequestHeaderSize(Integer.parseInt(_requestHeaderSize));
    }
    if (_responseBufferSize != null) {
      _client.setResponseBufferSize(Integer.parseInt(_responseBufferSize));
    }
    if (_responseHeaderSize != null) {
      _client.setResponseHeaderSize(Integer.parseInt(_responseHeaderSize));
    }
    _client.start();
  }
  
  private HttpURI proxyHttpURI(String uri)
    throws MalformedURLException
  {
    return new HttpURI(_proxyTo + uri);
  }
  
  protected String apply(String target, HttpServletRequest request, final HttpServletResponse response)
    throws IOException
  {
    synchronized (this)
    {
      if (_client == null) {
        try
        {
          initializeClient();
        }
        catch (Exception e)
        {
          throw new IOException("Unable to proxy: " + e.getMessage());
        }
      }
    }
    final int debug = _log.isDebugEnabled() ? request.hashCode() : 0;
    
    InputStream in = request.getInputStream();
    final Object out = response.getOutputStream();
    
    HttpURI url = createUrl(request, debug);
    if (url == null)
    {
      response.sendError(403);
      return target;
    }
    HttpExchange exchange = new HttpExchange()
    {
      protected void onRequestCommitted()
        throws IOException
      {}
      
      protected void onRequestComplete()
        throws IOException
      {}
      
      protected void onResponseComplete()
        throws IOException
      {
        if (debug != 0) {
          ProxyRule._log.debug(debug + " complete", new Object[0]);
        }
      }
      
      protected void onResponseContent(Buffer content)
        throws IOException
      {
        if (debug != 0) {
          ProxyRule._log.debug(debug + " content" + content.length(), new Object[0]);
        }
        content.writeTo(out);
      }
      
      protected void onResponseHeaderComplete()
        throws IOException
      {}
      
      protected void onResponseStatus(Buffer version, int status, Buffer reason)
        throws IOException
      {
        if (debug != 0) {
          ProxyRule._log.debug(debug + " " + version + " " + status + " " + reason, new Object[0]);
        }
        if ((reason != null) && (reason.length() > 0)) {
          response.setStatus(status, reason.toString());
        } else {
          response.setStatus(status);
        }
      }
      
      protected void onResponseHeader(Buffer name, Buffer value)
        throws IOException
      {
        String s = name.toString().toLowerCase();
        if ((!_DontProxyHeaders.contains(s)) || ((HttpHeaders.CONNECTION_BUFFER.equals(name)) && (HttpHeaderValues.CLOSE_BUFFER.equals(value))))
        {
          if (debug != 0) {
            ProxyRule._log.debug(debug + " " + name + ": " + value, new Object[0]);
          }
          response.addHeader(name.toString(), value.toString());
        }
        else if (debug != 0)
        {
          ProxyRule._log.debug(debug + " " + name + "! " + value, new Object[0]);
        }
      }
      
      protected void onConnectionFailed(Throwable ex)
      {
        ProxyRule._log.warn(ex.toString(), new Object[0]);
        ProxyRule._log.debug(ex);
        if (!response.isCommitted()) {
          response.setStatus(500);
        }
      }
      
      protected void onException(Throwable ex)
      {
        if ((ex instanceof EofException))
        {
          ProxyRule._log.ignore(ex);
          return;
        }
        ProxyRule._log.warn(ex.toString(), new Object[0]);
        ProxyRule._log.debug(ex);
        if (!response.isCommitted()) {
          response.setStatus(500);
        }
      }
      
      protected void onExpire()
      {
        if (!response.isCommitted()) {
          response.setStatus(504);
        }
      }
    };
    exchange.setMethod(request.getMethod());
    exchange.setURL(url.toString());
    exchange.setVersion(request.getProtocol());
    if (debug != 0) {
      _log.debug("{} {} {} {}", new Object[] { Integer.valueOf(debug), request.getMethod(), url, request.getProtocol() });
    }
    boolean hasContent = createHeaders(request, debug, exchange);
    if (hasContent) {
      exchange.setRequestContentSource(in);
    }
    long ctimeout = _client.getTimeout() > exchange.getTimeout() ? _client.getTimeout() : exchange.getTimeout();
    exchange.setTimeout(ctimeout);
    
    _client.send(exchange);
    try
    {
      exchange.waitForDone();
    }
    catch (InterruptedException e)
    {
      _log.info("Exception while waiting for response on proxied request", e);
    }
    return target;
  }
  
  private HttpURI createUrl(HttpServletRequest request, int debug)
    throws MalformedURLException
  {
    String uri = request.getRequestURI();
    if (request.getQueryString() != null) {
      uri = uri + "?" + request.getQueryString();
    }
    uri = PathMap.pathInfo(_pattern, uri);
    if (uri == null) {
      uri = "/";
    }
    HttpURI url = proxyHttpURI(uri);
    if (debug != 0) {
      _log.debug(debug + " proxy " + uri + "-->" + url, new Object[0]);
    }
    return url;
  }
  
  private boolean createHeaders(HttpServletRequest request, int debug, HttpExchange exchange)
  {
    String connectionHdr = request.getHeader("Connection");
    if (connectionHdr != null)
    {
      connectionHdr = connectionHdr.toLowerCase();
      if ((connectionHdr.indexOf("keep-alive") < 0) && (connectionHdr.indexOf("close") < 0)) {
        connectionHdr = null;
      }
    }
    if (_hostHeader != null) {
      exchange.setRequestHeader("Host", _hostHeader);
    }
    boolean xForwardedFor = false;
    boolean hasContent = false;
    long contentLength = -1L;
    Enumeration<?> enm = request.getHeaderNames();
    while (enm.hasMoreElements())
    {
      String hdr = (String)enm.nextElement();
      String lhdr = hdr.toLowerCase();
      if ((!_DontProxyHeaders.contains(lhdr)) && 
      
        ((connectionHdr == null) || (connectionHdr.indexOf(lhdr) < 0)) && (
        
        (_hostHeader == null) || (!"host".equals(lhdr))))
      {
        if ("content-type".equals(lhdr))
        {
          hasContent = true;
        }
        else if ("content-length".equals(lhdr))
        {
          contentLength = request.getContentLength();
          exchange.setRequestHeader("Content-Length", Long.toString(contentLength));
          if (contentLength > 0L) {
            hasContent = true;
          }
        }
        else if ("x-forwarded-for".equals(lhdr))
        {
          xForwardedFor = true;
        }
        Enumeration<?> vals = request.getHeaders(hdr);
        while (vals.hasMoreElements())
        {
          String val = (String)vals.nextElement();
          if (val != null)
          {
            if (debug != 0) {
              _log.debug("{} {} {}", new Object[] { Integer.valueOf(debug), hdr, val });
            }
            exchange.setRequestHeader(hdr, val);
          }
        }
      }
    }
    exchange.setRequestHeader("Via", "1.1 (jetty)");
    if (!xForwardedFor)
    {
      exchange.addRequestHeader("X-Forwarded-For", request.getRemoteAddr());
      exchange.addRequestHeader("X-Forwarded-Proto", request.getScheme());
      exchange.addRequestHeader("X-Forwarded-Host", request.getServerName());
      exchange.addRequestHeader("X-Forwarded-Server", request.getLocalName());
    }
    return hasContent;
  }
  
  public void setProxyTo(String proxyTo)
  {
    _proxyTo = proxyTo;
  }
  
  public void setMaxThreads(String maxThreads)
  {
    _maxThreads = maxThreads;
  }
  
  public void setMaxConnections(String maxConnections)
  {
    _maxConnections = maxConnections;
  }
  
  public void setTimeout(String timeout)
  {
    _timeout = 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd