View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.rewrite.handler;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.http.Cookie;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.util.annotation.Name;
28  
29  
30  /**
31   * Sets the cookie in the response whenever the rule finds a match.
32   * 
33   * @see Cookie
34   */
35  public class CookiePatternRule extends PatternRule
36  {
37      private String _name;
38      private String _value;
39  
40      /* ------------------------------------------------------------ */
41      public CookiePatternRule()
42      {
43          this(null,null,null);
44      }
45      
46      /* ------------------------------------------------------------ */
47      public CookiePatternRule(@Name("pattern") String pattern, @Name("name") String name, @Name("value") String value)
48      {
49          super(pattern);
50          _handling = false;
51          _terminating = false;
52          setName(name);
53          setValue(value);
54      }
55  
56      /* ------------------------------------------------------------ */
57      /**
58       * Assigns the cookie name.
59       * 
60       * @param name a <code>String</code> specifying the name of the cookie.
61       */
62      public void setName(String name)
63      {
64          _name = name;
65      }
66  
67      /* ------------------------------------------------------------ */
68      /**
69       * Assigns the cookie value.
70       * 
71       * @param value a <code>String</code> specifying the value of the cookie
72       * @see Cookie#setValue(String)
73       */
74      public void setValue(String value)
75      {
76          _value = value;
77      }
78  
79      /* ------------------------------------------------------------ */
80      /*
81       * (non-Javadoc)
82       * @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
83       */
84      @Override
85      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
86      {
87          // Check that cookie is not already set
88          Cookie[] cookies = request.getCookies();
89          if (cookies!=null)
90          {
91              for (Cookie cookie:cookies)
92              {
93                  if (_name.equals(cookie.getName()) && _value.equals(cookie.getValue()))
94                      return target;
95              }
96          }
97          
98          // set it
99          response.addCookie(new Cookie(_name, _value));
100         return target;
101     }
102 
103     /* ------------------------------------------------------------ */
104     /**
105      * Returns the cookie contents.
106      */
107     @Override
108     public String toString()
109     {
110         return super.toString()+"["+_name+","+_value + "]";
111     }
112 }