WebUtils.java
4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig 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 the following location:
*
* 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.jasig.cas.web.support;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jasig.cas.authentication.principal.WebApplicationService;
import org.springframework.util.Assert;
import org.springframework.webflow.context.servlet.ServletExternalContext;
import org.springframework.webflow.execution.RequestContext;
/**
* Common utilities for the web tier.
*
* @author Scott Battaglia
* @version $Revision$ $Date$
* @since 3.1
*/
public final class WebUtils {
/** Request attribute that contains message key describing details of authorization failure.*/
public static final String CAS_ACCESS_DENIED_REASON = "CAS_ACCESS_DENIED_REASON";
public static HttpServletRequest getHttpServletRequest(
final RequestContext context) {
Assert.isInstanceOf(ServletExternalContext.class, context
.getExternalContext(),
"Cannot obtain HttpServletRequest from event of type: "
+ context.getExternalContext().getClass().getName());
return (HttpServletRequest) context.getExternalContext().getNativeRequest();
}
public static HttpServletResponse getHttpServletResponse(
final RequestContext context) {
Assert.isInstanceOf(ServletExternalContext.class, context
.getExternalContext(),
"Cannot obtain HttpServletResponse from event of type: "
+ context.getExternalContext().getClass().getName());
return (HttpServletResponse) context.getExternalContext()
.getNativeResponse();
}
public static WebApplicationService getService(
final List<ArgumentExtractor> argumentExtractors,
final HttpServletRequest request) {
for (final ArgumentExtractor argumentExtractor : argumentExtractors) {
final WebApplicationService service = argumentExtractor
.extractService(request);
if (service != null) {
return service;
}
}
return null;
}
public static WebApplicationService getService(
final List<ArgumentExtractor> argumentExtractors,
final RequestContext context) {
final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
return getService(argumentExtractors, request);
}
public static WebApplicationService getService(
final RequestContext context) {
return (WebApplicationService) context.getFlowScope().get("service");
}
public static void putTicketGrantingTicketInRequestScope(
final RequestContext context, final String ticketValue) {
context.getRequestScope().put("ticketGrantingTicketId", ticketValue);
}
public static String getTicketGrantingTicketId(
final RequestContext context) {
final String tgtFromRequest = (String) context.getRequestScope().get("ticketGrantingTicketId");
final String tgtFromFlow = (String) context.getFlowScope().get("ticketGrantingTicketId");
return tgtFromRequest != null ? tgtFromRequest : tgtFromFlow;
}
public static void putServiceTicketInRequestScope(
final RequestContext context, final String ticketValue) {
context.getRequestScope().put("serviceTicketId", ticketValue);
}
public static String getServiceTicketFromRequestScope(
final RequestContext context) {
return context.getRequestScope().getString("serviceTicketId");
}
public static void putLoginTicket(final RequestContext context, final String ticket) {
context.getFlowScope().put("loginTicket", ticket);
}
public static String getLoginTicketFromFlowScope(final RequestContext context) {
// Getting the saved LT destroys it in support of one-time-use
// See section 3.5.1 of http://www.jasig.org/cas/protocol
final String lt = (String) context.getFlowScope().remove("loginTicket");
return lt != null ? lt : "";
}
public static String getLoginTicketFromRequest(final RequestContext context) {
return context.getRequestParameters().get("lt");
}
}