Coverage Report - org.apache.shiro.web.subject.support.DefaultWebSubjectContext
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultWebSubjectContext
100%
32/32
62%
10/16
1.889
 
 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  
 package org.apache.shiro.web.subject.support;
 20  
 
 21  
 import org.apache.shiro.subject.Subject;
 22  
 import org.apache.shiro.subject.support.DefaultSubjectContext;
 23  
 import org.apache.shiro.web.subject.WebSubject;
 24  
 import org.apache.shiro.web.subject.WebSubjectContext;
 25  
 
 26  
 import javax.servlet.ServletRequest;
 27  
 import javax.servlet.ServletResponse;
 28  
 
 29  
 /**
 30  
  * Default {@code WebSubjectContext} implementation that provides for additional storage and retrieval of
 31  
  * a {@link ServletRequest} and {@link ServletResponse}.
 32  
  *
 33  
  * @since 1.0
 34  
  */
 35  
 public class DefaultWebSubjectContext extends DefaultSubjectContext implements WebSubjectContext {
 36  
 
 37  
     private static final long serialVersionUID = 8188555355305827739L;
 38  
 
 39  1
     private static final String SERVLET_REQUEST = DefaultWebSubjectContext.class.getName() + ".SERVLET_REQUEST";
 40  1
     private static final String SERVLET_RESPONSE = DefaultWebSubjectContext.class.getName() + ".SERVLET_RESPONSE";
 41  
 
 42  9
     public DefaultWebSubjectContext() {
 43  9
     }
 44  
 
 45  
     public DefaultWebSubjectContext(WebSubjectContext context) {
 46  6
         super(context);
 47  6
     }
 48  
 
 49  
     @Override
 50  
     public String resolveHost() {
 51  6
         String host = super.resolveHost();
 52  6
         if (host == null) {
 53  6
             ServletRequest request = resolveServletRequest();
 54  6
             if (request != null) {
 55  6
                 host = request.getRemoteHost();
 56  
             }
 57  
         }
 58  6
         return host;
 59  
     }
 60  
 
 61  
     public ServletRequest getServletRequest() {
 62  49
         return getTypedValue(SERVLET_REQUEST, ServletRequest.class);
 63  
     }
 64  
 
 65  
     public void setServletRequest(ServletRequest request) {
 66  8
         if (request != null) {
 67  8
             put(SERVLET_REQUEST, request);
 68  
         }
 69  8
     }
 70  
 
 71  
     public ServletRequest resolveServletRequest() {
 72  
 
 73  20
         ServletRequest request = getServletRequest();
 74  
 
 75  
         //fall back on existing subject instance if it exists:
 76  20
         if (request == null) {
 77  2
             Subject existing = getSubject();
 78  2
             if (existing instanceof WebSubject) {
 79  2
                 request = ((WebSubject) existing).getServletRequest();
 80  
             }
 81  
         }
 82  
 
 83  20
         return request;
 84  
     }
 85  
 
 86  
     public ServletResponse getServletResponse() {
 87  35
         return getTypedValue(SERVLET_RESPONSE, ServletResponse.class);
 88  
     }
 89  
 
 90  
     public void setServletResponse(ServletResponse response) {
 91  8
         if (response != null) {
 92  8
             put(SERVLET_RESPONSE, response);
 93  
         }
 94  8
     }
 95  
 
 96  
     public ServletResponse resolveServletResponse() {
 97  
 
 98  6
         ServletResponse response = getServletResponse();
 99  
 
 100  
         //fall back on existing subject instance if it exists:
 101  6
         if (response == null) {
 102  1
             Subject existing = getSubject();
 103  1
             if (existing instanceof WebSubject) {
 104  1
                 response = ((WebSubject) existing).getServletResponse();
 105  
             }
 106  
         }
 107  
 
 108  6
         return response;
 109  
     }
 110  
 }