| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>WebSubject.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Jar Bundle</a> > <a href="../index.html" class="el_bundle">shiro-web</a> > <a href="index.source.html" class="el_package">org.apache.shiro.web.subject</a> > <span class="el_source">WebSubject.java</span></div><h1>WebSubject.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.web.subject; |
| |
| import org.apache.shiro.SecurityUtils; |
| import org.apache.shiro.mgt.SecurityManager; |
| import org.apache.shiro.subject.Subject; |
| import org.apache.shiro.subject.SubjectContext; |
| import org.apache.shiro.web.subject.support.DefaultWebSubjectContext; |
| import org.apache.shiro.web.util.RequestPairSource; |
| |
| import javax.servlet.ServletRequest; |
| import javax.servlet.ServletResponse; |
| |
| /** |
| * A {@code WebSubject} represents a Subject instance that was acquired as a result of an incoming |
| * {@link ServletRequest}. |
| * |
| * @since 1.0 |
| */ |
| public interface WebSubject extends Subject, RequestPairSource { |
| |
| /** |
| * Returns the {@code ServletRequest} accessible when the Subject instance was created. |
| * |
| * @return the {@code ServletRequest} accessible when the Subject instance was created. |
| */ |
| ServletRequest getServletRequest(); |
| |
| /** |
| * Returns the {@code ServletResponse} accessible when the Subject instance was created. |
| * |
| * @return the {@code ServletResponse} accessible when the Subject instance was created. |
| */ |
| ServletResponse getServletResponse(); |
| |
| /** |
| * A {@code WebSubject.Builder} performs the same function as a {@link Subject.Builder Subject.Builder}, but |
| * additionally ensures that the Servlet request/response pair that is triggering the Subject instance's creation |
| * is retained for use by internal Shiro components as necessary. |
| */ |
| public static class Builder extends Subject.Builder { |
| |
| /** |
| * Constructs a new {@code Web.Builder} instance using the {@link SecurityManager SecurityManager} obtained by |
| * calling {@code SecurityUtils.}{@link SecurityUtils#getSecurityManager() getSecurityManager()}. If you want |
| * to specify your own SecurityManager instance, use the |
| * {@link #Builder(SecurityManager, ServletRequest, ServletResponse)} constructor instead. |
| * |
| * @param request the incoming ServletRequest that will be associated with the built {@code WebSubject} instance. |
| * @param response the outgoing ServletRequest paired with the ServletRequest that will be associated with the |
| * built {@code WebSubject} instance. |
| */ |
| public Builder(ServletRequest request, ServletResponse response) { |
| <span class="nc" id="L71"> this(SecurityUtils.getSecurityManager(), request, response);</span> |
| <span class="nc" id="L72"> }</span> |
| |
| /** |
| * Constructs a new {@code Web.Builder} instance using the specified {@code SecurityManager} instance to |
| * create the {@link WebSubject WebSubject} instance. |
| * |
| * @param securityManager the {@code SecurityManager SecurityManager} instance to use to build the |
| * {@code WebSubject} instance. |
| * @param request the incoming ServletRequest that will be associated with the built {@code WebSubject} |
| * instance. |
| * @param response the outgoing ServletRequest paired with the ServletRequest that will be associated |
| * with the built {@code WebSubject} instance. |
| */ |
| public Builder(SecurityManager securityManager, ServletRequest request, ServletResponse response) { |
| <span class="fc" id="L86"> super(securityManager);</span> |
| <span class="pc bpc" id="L87" title="1 of 2 branches missed."> if (request == null) {</span> |
| <span class="nc" id="L88"> throw new IllegalArgumentException("ServletRequest argument cannot be null.");</span> |
| } |
| <span class="pc bpc" id="L90" title="1 of 2 branches missed."> if (response == null) {</span> |
| <span class="nc" id="L91"> throw new IllegalArgumentException("ServletResponse argument cannot be null.");</span> |
| } |
| <span class="fc" id="L93"> setRequest(request);</span> |
| <span class="fc" id="L94"> setResponse(response);</span> |
| <span class="fc" id="L95"> }</span> |
| |
| /** |
| * Overrides the parent implementation to return a new instance of a |
| * {@link DefaultWebSubjectContext DefaultWebSubjectContext} to account for the additional request/response |
| * pair. |
| * |
| * @return a new instance of a {@link DefaultWebSubjectContext DefaultWebSubjectContext} to account for the |
| * additional request/response pair. |
| */ |
| @Override |
| protected SubjectContext newSubjectContextInstance() { |
| <span class="fc" id="L107"> return new DefaultWebSubjectContext();</span> |
| } |
| |
| /** |
| * Called by the {@code WebSubject.Builder} constructor, this method places the request object in the |
| * context map for later retrieval. |
| * |
| * @param request the incoming ServletRequest that triggered the creation of the {@code WebSubject} instance. |
| * @return 'this' for method chaining. |
| */ |
| protected Builder setRequest(ServletRequest request) { |
| <span class="pc bpc" id="L118" title="1 of 2 branches missed."> if (request != null) {</span> |
| <span class="fc" id="L119"> ((WebSubjectContext) getSubjectContext()).setServletRequest(request);</span> |
| } |
| <span class="fc" id="L121"> return this;</span> |
| } |
| |
| /** |
| * Called by the {@code WebSubject.Builder} constructor, this method places the response object in the |
| * context map for later retrieval. |
| * |
| * @param response the outgoing ServletRequest paired with the ServletRequest that triggered the creation of |
| * the {@code WebSubject} instance. |
| * @return 'this' for method chaining. |
| */ |
| protected Builder setResponse(ServletResponse response) { |
| <span class="pc bpc" id="L133" title="1 of 2 branches missed."> if (response != null) {</span> |
| <span class="fc" id="L134"> ((WebSubjectContext) getSubjectContext()).setServletResponse(response);</span> |
| } |
| <span class="fc" id="L136"> return this;</span> |
| } |
| |
| /** |
| * Returns {@link #buildSubject() super.buildSubject()}, but additionally ensures that the returned instance |
| * is an {@code instanceof} {@link WebSubject WebSubject} and to support a type-safe method so a caller |
| * does not have to cast. Per the parent class's method JavaDoc, this method will return a new instance |
| * each time it is called. |
| * |
| * @return a new {@link WebSubject WebSubject} instance built by this {@code Builder}. |
| */ |
| public WebSubject buildWebSubject() { |
| <span class="fc" id="L148"> Subject subject = super.buildSubject();</span> |
| <span class="pc bpc" id="L149" title="1 of 2 branches missed."> if (!(subject instanceof WebSubject)) {</span> |
| <span class="nc" id="L150"> String msg = "Subject implementation returned from the SecurityManager was not a " +</span> |
| <span class="nc" id="L151"> WebSubject.class.getName() + " implementation. Please ensure a Web-enabled SecurityManager " +</span> |
| "has been configured and made available to this builder."; |
| <span class="nc" id="L153"> throw new IllegalStateException(msg);</span> |
| } |
| <span class="fc" id="L155"> return (WebSubject) subject;</span> |
| } |
| } |
| |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |