SQOOP-2823: Sqoop2: RESTiliency: Remove repetitive try-catch block for accessing...
[sqoop.git] / server / src / main / java / org / apache / sqoop / server / RequestContext.java
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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.sqoop.server;
19
20 import org.apache.hadoop.security.authentication.client.PseudoAuthenticator;
21 import org.apache.hadoop.security.token.delegation.web.HttpUserGroupInformation;
22 import org.apache.sqoop.common.SqoopException;
23 import org.apache.sqoop.core.SqoopConfiguration;
24 import org.apache.sqoop.security.AuthenticationManager;
25 import org.apache.sqoop.security.SecurityConstants;
26 import org.apache.sqoop.server.common.ServerError;
27 import org.apache.sqoop.utils.UrlSafeUtils;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.util.Locale;
34
35 public class RequestContext {
36
37 /**
38 * Enumeration with supported HTTP methods.
39 */
40 public enum Method {
41 GET,
42 POST,
43 PUT,
44 DELETE,
45 }
46
47 private final HttpServletRequest request;
48 private final HttpServletResponse response;
49
50 public RequestContext(HttpServletRequest req, HttpServletResponse resp) {
51 request = req;
52 response = resp;
53 }
54
55 public HttpServletRequest getRequest() {
56 return request;
57 }
58
59 public HttpServletResponse getResponse() {
60 return response;
61 }
62
63 public String getPath() {
64 return request.getRequestURL().toString();
65 }
66
67 /**
68 * Get method that was used for this HTTP request.
69 *
70 * @return Method that was used
71 */
72 public Method getMethod() {
73 try {
74 return Method.valueOf(request.getMethod());
75 } catch(IllegalArgumentException ex) {
76 throw new SqoopException(ServerError.SERVER_0002,
77 "Unsupported HTTP method:" + request.getMethod(), ex);
78 }
79 }
80
81 public BufferedReader getReader() {
82 try {
83 return request.getReader();
84 } catch (IOException e) {
85 throw new SqoopException(ServerError.SERVER_0003, "Expected data stream", e);
86 }
87 }
88
89 /**
90 * Return last element of URL.
91 *
92 * Return text occurring after last "/" character in URL, typically there will
93 * be an ID.
94 *
95 * @return String after last "/" in URL
96 */
97 public String getLastURLElement() {
98 String uri = getRequest().getRequestURI();
99 int slash = uri.lastIndexOf('/');
100 return UrlSafeUtils.urlPathDecode(uri.substring(slash + 1));
101 }
102
103 /**
104 * Return all elements in the url as an array
105 */
106 public String[] getUrlElements() {
107 String[] elements = getRequest().getRequestURI().split("/");
108 for(int i = 0; i < elements.length; i++) {
109 elements[i] = UrlSafeUtils.urlPathDecode(elements[i]);
110 }
111 return elements;
112 }
113
114 /**
115 * Return a value for given query parameter name
116 */
117 public String getParameterValue(String name) {
118 String[] values = getRequest().getParameterValues(name);
119 String value = values != null ? values[0] : null;
120 if (value != null) {
121 value = UrlSafeUtils.urlDecode(value);
122 }
123 return value;
124 }
125
126 /**
127 * Get locale specified in accept-language HTTP header.
128 *
129 * @return First specified locale
130 */
131 public Locale getAcceptLanguageHeader() {
132 String lang = request.getHeader("Accept-Language");
133 if (lang == null) {
134 lang = Locale.getDefault().getLanguage();
135 }
136 return new Locale(lang);
137 }
138
139 /**
140 * Get username specified by custom username HTTP header.
141 *
142 * @return Name of user sending the request
143 */
144 public String getUserName() {
145 String userName;
146 if (AuthenticationManager.getInstance().getAuthenticationHandler().isSecurityEnabled()) {
147 userName = HttpUserGroupInformation.get().getShortUserName();
148 } else {
149 userName = request.getParameter(PseudoAuthenticator.USER_NAME);
150 }
151
152 if (userName == null || userName.trim().isEmpty()) {
153 userName = SqoopConfiguration.getInstance().getContext().getString(
154 SecurityConstants.AUTHENTICATION_DEFAULT_USER,
155 SecurityConstants.AUTHENTICATION_DEFAULT_USER_DEFAULT);
156 }
157 return userName;
158 }
159 }