View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.proxy.session;
    21
    22  import java.net.InetSocketAddress;
    23  import java.nio.charset.Charset;
    24  import java.util.List;
    25
    26  import org.apache.mina.core.session.IoSession;
    27  import org.apache.mina.proxy.ProxyConnector;
    28  import org.apache.mina.proxy.ProxyLogicHandler;
    29  import org.apache.mina.proxy.event.IoSessionEventQueue;
    30  import org.apache.mina.proxy.filter.ProxyFilter;
    31  import org.apache.mina.proxy.handlers.ProxyRequest;
    32  import org.apache.mina.proxy.handlers.http.HttpAuthenticationMethods;
    33  import org.apache.mina.proxy.handlers.http.HttpSmartProxyHandler;
    34
    35  /**
    36   * ProxyIoSession.java - Class that contains all informations for the current proxy
    37   * authentication session.
    38   *
    39   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    40   * @since MINA 2.0.0-M3
    41   */
    42  public class ProxyIoSession {
    43
    44      public final static String PROXY_SESSION = ProxyConnector.class.getName() + ".ProxySession";
    45
    46      private final static String DEFAULT_ENCODING = "ISO-8859-1";
    47
    48      /**
    49       * The list contains the authentication methods to use.
    50       * The order in the list is revelant : if first method is available
    51       * then it will be used etc ...
    52       */
    53      private List<HttpAuthenticationMethods> preferedOrder;
    54
    55      /**
    56       * The request to send to the proxy.
    57       */
    58      private ProxyRequest request;
    59
    60      /**
    61       * The currently selected proxy handler.
    62       */
    63      private ProxyLogicHandler handler;
    64
    65      /**
    66       * Parent {@link ProxyFilter} handling the session.
    67       */
    68      private ProxyFilter proxyFilter;
    69
    70      /**
    71       * The session.
    72       */
    73      private IoSession session;
    74
    75      /**
    76       * The proxy connector.
    77       */
    78      private ProxyConnector connector;
    79
    80      /**
    81       * Address of the proxy server.
    82       */
    83      private InetSocketAddress proxyAddress = null;
    84
    85      /**
    86       * A flag that indicates that the proxy closed the connection before handshake
    87       * is done. So we need to reconnect to the proxy to continue the handshaking
    88       * process.
    89       */
    90      private boolean reconnectionNeeded = false;
    91
    92      /**
    93       * Name of the charset used for string encoding & decoding.
    94       */
    95      private String charsetName;
    96
    97      /**
    98       * The session event queue.
    99       */
    100     private IoSessionEventQueue eventQueue = new IoSessionEventQueue(this);
    101
    102     /**
    103      * Set to true when an exception has been thrown or if authentication failed.
    104      */
    105     private boolean authenticationFailed;
    106
    107     /**
    108      * Constructor.
    109      *
    110      * @param proxyAddress the IP address of the proxy server
    111      * @param request the proxy request
    112      */
    113     public ProxyIoSession(InetSocketAddress proxyAddress, ProxyRequest request) {
    114         setProxyAddress(proxyAddress);
    115         setRequest(request);
    116     }
    117
    118     /**
    119      * Returns the pending event queue.
    120      */
    121     public IoSessionEventQueue getEventQueue() {
    122         return eventQueue;
    123     }
    124
    125     /**
    126      * Returns the list of the prefered order for the authentication methods.
    127      * This list is used by the {@link HttpSmartProxyHandler} to determine
    128      * which authentication mechanism to use first between those accepted by the
    129      * proxy server. This list is only used when connecting to an http proxy.
    130      */
    131     public List<HttpAuthenticationMethods> getPreferedOrder() {
    132         return preferedOrder;
    133     }
    134
    135     /**
    136      * Sets the ordered list of prefered authentication mechanisms.
    137      *
    138      * @param preferedOrder the ordered list
    139      */
    140     public void setPreferedOrder(List<HttpAuthenticationMethods> preferedOrder) {
    141         this.preferedOrder = preferedOrder;
    142     }
    143
    144     /**
    145      * Returns the {@link ProxyLogicHandler} currently in use.
    146      */
    147     public ProxyLogicHandler getHandler() {
    148         return handler;
    149     }
    150
    151     /**
    152      * Sets the {@link ProxyLogicHandler} to use.
    153      *
    154      * @param handler the {@link ProxyLogicHandler} instance
    155      */
    156     public void setHandler(ProxyLogicHandler handler) {
    157         this.handler = handler;
    158     }
    159
    160     /**
    161      * Returns the {@link ProxyFilter}.
    162      */
    163     public ProxyFilter getProxyFilter() {
    164         return proxyFilter;
    165     }
    166
    167     /**
    168      * Sets the {@link ProxyFilter}.
    169      * Note : Please do not call this method from your code it could result
    170      * in an unexpected behaviour.
    171      *
    172      * @param proxyFilter the filter
    173      */
    174     public void setProxyFilter(ProxyFilter proxyFilter) {
    175         this.proxyFilter = proxyFilter;
    176     }
    177
    178     /**
    179      * Returns the proxy request.
    180      */
    181     public ProxyRequest getRequest() {
    182         return request;
    183     }
    184
    185     /**
    186      * Sets the proxy request.
    187      *
    188      * @param request the proxy request
    189      */
    190     private void setRequest(ProxyRequest request) {
    191         if (request == null) {
    192             throw new IllegalArgumentException("request cannot be null");
    193         }
    194
    195         this.request = request;
    196     }
    197
    198     /**
    199      * Returns the current {@link IoSession}.
    200      */
    201     public IoSession getSession() {
    202         return session;
    203     }
    204
    205     /**
    206      * Sets the {@link IoSession} in use.
    207      * Note : Please do not call this method from your code it could result in an
    208      * unexpected behaviour.
    209      *
    210      * @param session the current io session
    211      */
    212     public void setSession(IoSession session) {
    213         this.session = session;
    214     }
    215
    216     /**
    217      * Returns the proxy connector.
    218      */
    219     public ProxyConnector getConnector() {
    220         return connector;
    221     }
    222
    223     /**
    224      * Sets the connector reference of this proxy session.
    225      * Note : Please do not call this method from your code it could result in an
    226      * unexpected behaviour.
    227      *
    228      * @param connector the proxy connector
    229      */
    230     public void setConnector(ProxyConnector connector) {
    231         this.connector = connector;
    232     }
    233
    234     /**
    235      * Returns the IP address of the proxy server.
    236      */
    237     public InetSocketAddress getProxyAddress() {
    238         return proxyAddress;
    239     }
    240
    241     /**
    242      * Sets the IP address of the proxy server.
    243      *
    244      * @param proxyAddress the IP address of the proxy server
    245      */
    246     private void setProxyAddress(InetSocketAddress proxyAddress) {
    247         if (proxyAddress == null) {
    248             throw new IllegalArgumentException("proxyAddress object cannot be null");
    249         }
    250
    251         this.proxyAddress = proxyAddress;
    252     }
    253
    254     /**
    255      * Returns true if the current authentication process is not finished
    256      * but the server has closed the connection.
    257      */
    258     public boolean isReconnectionNeeded() {
    259         return reconnectionNeeded;
    260     }
    261
    262     /**
    263      * Sets the reconnection needed flag. If set to true, it means that an
    264      * authentication process is currently running but the proxy server did not
    265      * kept the connection alive. So we need to reconnect to the server to complete
    266      * the process.
    267      * Note : Please do not call this method from your code it could result in an
    268      * unexpected behaviour.
    269      *
    270      * @param reconnectionNeeded the value to set the flag to
    271      */
    272     public void setReconnectionNeeded(boolean reconnectionNeeded) {
    273         this.reconnectionNeeded = reconnectionNeeded;
    274     }
    275
    276     /**
    277      * Returns a charset instance of the in use charset name.
    278      */
    279     public Charset getCharset() {
    280         return Charset.forName(getCharsetName());
    281     }
    282
    283     /**
    284      * Returns the used charset name or {@link #DEFAULT_ENCODING} if null.
    285      */
    286     public String getCharsetName() {
    287         if (charsetName == null) {
    288             charsetName = DEFAULT_ENCODING;
    289         }
    290
    291         return charsetName;
    292     }
    293
    294     /**
    295      * Sets the charset to use.
    296      *
    297      * @param charsetName the charset name
    298      */
    299     public void setCharsetName(String charsetName) {
    300         this.charsetName = charsetName;
    301     }
    302
    303     /**
    304      * Returns true if authentication failed.
    305      */
    306     public boolean isAuthenticationFailed() {
    307         return authenticationFailed;
    308     }
    309
    310     /**
    311      * Sets the authentication failed flag.
    312      *
    313      * @param authenticationFailed the value to set the flag to
    314      */
    315     public void setAuthenticationFailed(boolean authenticationFailed) {
    316         this.authenticationFailed = authenticationFailed;
    317     }
    318 }