|
| 1 | +/* |
| 2 | + * Copyright 2013 David M. Johnson ([email protected]). |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package snoopware.api; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Properties; |
| 20 | +import java.util.logging.Level; |
| 21 | +import java.util.logging.Logger; |
| 22 | +import javax.servlet.ServletConfig; |
| 23 | +import javax.servlet.ServletException; |
| 24 | +import javax.servlet.annotation.WebInitParam; |
| 25 | +import javax.servlet.annotation.WebServlet; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import org.apache.http.HttpRequest; |
| 28 | +import org.usergrid.java.client.Client; |
| 29 | + |
| 30 | +/** |
| 31 | + * Proxy all requests to UserGrid and to add credentials to /users GET and POST requests. |
| 32 | + * @author David M. Johnson ([email protected]) |
| 33 | + */ |
| 34 | +@WebServlet( |
| 35 | + name = "usergridProxy", urlPatterns = {"/api/*"}, |
| 36 | + initParams={ |
| 37 | + @WebInitParam(name="log", value="true"), |
| 38 | + @WebInitParam(name="targetUri", value="https://api.usergrid.com") |
| 39 | + }) |
| 40 | +@SuppressWarnings("serial") |
| 41 | +public class UserGridProxyServlet extends ProxyServlet { |
| 42 | + protected static final Logger log = Logger.getLogger(ProxyServlet.class.getName()); |
| 43 | + |
| 44 | + private Client userGridClient; |
| 45 | + private String applicationName; |
| 46 | + |
| 47 | + /** |
| 48 | + * Override to authorized UserGrid client with creds from app.propeties. |
| 49 | + */ |
| 50 | + @Override |
| 51 | + public void init(ServletConfig servletConfig) throws ServletException { |
| 52 | + super.init(servletConfig); |
| 53 | + Properties properties = new Properties(); |
| 54 | + try { |
| 55 | + properties.load(getClass().getResourceAsStream("/app.properties")); |
| 56 | + |
| 57 | + applicationName = (String)properties.get("app.applicationName"); |
| 58 | + String organizationId = (String)properties.get("app.organizationId"); |
| 59 | + String applicationId = (String)properties.get("app.applicationId"); |
| 60 | + userGridClient = new Client(organizationId, applicationId); |
| 61 | + |
| 62 | + String clientId = (String)properties.get("app.clientId"); |
| 63 | + String clientSecret = (String)properties.get("app.clientSecret"); |
| 64 | + userGridClient.authorizeAppClient(clientId, clientSecret); |
| 65 | + |
| 66 | + } catch (IOException ex) { |
| 67 | + log.log(Level.SEVERE, "Unable to load app.properties and authorize the client", ex); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Override to add Authorization to requests that need it. |
| 73 | + */ |
| 74 | + @Override |
| 75 | + protected void copyRequestHeaders(HttpServletRequest req, HttpRequest proxyReq) { |
| 76 | + super.copyRequestHeaders(req, proxyReq); |
| 77 | + |
| 78 | + boolean hasAccessToken = req.getQueryString() == null |
| 79 | + ? false : req.getQueryString().contains("access_token="); |
| 80 | + |
| 81 | + if (!hasAccessToken |
| 82 | + && ("POST".equals(req.getMethod()) || "GET".equals(req.getMethod())) |
| 83 | + && req.getPathInfo().contains("/" + applicationName + "/users")) { |
| 84 | + |
| 85 | + String header = "Bearer " + userGridClient.getAccessToken(); |
| 86 | + proxyReq.setHeader("Authorization", header); |
| 87 | + log.log(Level.INFO, "Added header = {0}", header); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments