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  package org.apache.shiro.samples.spring.web;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.springframework.web.servlet.ModelAndView;
27  import org.springframework.web.servlet.mvc.AbstractController;
28  
29  import org.apache.shiro.SecurityUtils;
30  import org.apache.shiro.session.Session;
31  import org.apache.shiro.subject.Subject;
32  
33  /**
34   * Controller used to dynamically build a JNLP file used to launch the Shiro
35   * Spring WebStart sample application.
36   *
37   * @since 0.1
38   */
39  public class JnlpController extends AbstractController {
40  
41      /*--------------------------------------------
42      |             C O N S T A N T S             |
43      ============================================*/
44  
45      /*--------------------------------------------
46      |    I N S T A N C E   V A R I A B L E S    |
47      ============================================*/
48      private String jnlpView;
49  
50      /*--------------------------------------------
51      |         C O N S T R U C T O R S           |
52      ============================================*/
53  
54      /*--------------------------------------------
55      |  A C C E S S O R S / M O D I F I E R S    |
56      ============================================*/
57  
58      public void setJnlpView(String jnlpView) {
59          this.jnlpView = jnlpView;
60      }
61  
62      /*--------------------------------------------
63      |               M E T H O D S               |
64      ============================================*/
65  
66      protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
67  
68          Subject subject = SecurityUtils.getSubject();
69          Session session = null;
70  
71          if (subject != null) {
72              session = subject.getSession();
73          }
74          if (session == null) {
75              String msg = "Expected a non-null Shiro session.";
76              throw new IllegalArgumentException(msg);
77          }
78  
79          StringBuilder sb = new StringBuilder();
80          sb.append("http://");
81          sb.append(request.getServerName());
82          if (request.getServerPort() != 80) {
83              sb.append(":");
84              sb.append(request.getServerPort());
85          }
86          sb.append(request.getContextPath());
87  
88          // prevent JNLP caching by setting response headers
89          response.setHeader("cache-control", "no-cache");
90          response.setHeader("pragma", "no-cache");
91  
92          Map<String, Object> model = new HashMap<String, Object>();
93          model.put("codebaseUrl", sb.toString());
94          model.put("sessionId", session.getId());
95          return new ModelAndView(jnlpView, model);
96      }
97  }