|
| 1 | +/* |
| 2 | + * TeleStax, Open Source Cloud Communications |
| 3 | + * Copyright 2011-2014, Telestax Inc and individual contributors |
| 4 | + * by the @authors tag. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation; either version 3 of |
| 9 | + * the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 18 | + * |
| 19 | + */ |
| 20 | +package org.restcomm.connect.http.filters; |
| 21 | + |
| 22 | +import com.sun.jersey.spi.container.ContainerRequest; |
| 23 | +import com.sun.jersey.spi.container.ContainerRequestFilter; |
| 24 | +import com.sun.jersey.spi.container.ContainerResponseFilter; |
| 25 | +import com.sun.jersey.spi.container.ResourceFilter; |
| 26 | +import java.net.URI; |
| 27 | +import javax.ws.rs.ext.Provider; |
| 28 | +import org.apache.log4j.Logger; |
| 29 | +import org.restcomm.connect.commons.util.UriUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * Ensures UriInfo builders build proper link scheme depending on container |
| 33 | + * connector (http/https) |
| 34 | + * |
| 35 | + * This will apply to any jaxrs endpoint trying to build uris later. |
| 36 | + * |
| 37 | + * UriUtils is used to discover connectors in runtime, and check the scheme. |
| 38 | + * |
| 39 | + */ |
| 40 | +@Provider |
| 41 | +public class SchemeRewriteFilter implements ResourceFilter, ContainerRequestFilter { |
| 42 | + |
| 43 | + protected Logger logger = Logger.getLogger(SchemeRewriteFilter.class); |
| 44 | + |
| 45 | + private static final String HTTPS_SCHEME = "https"; |
| 46 | + |
| 47 | + |
| 48 | + @Override |
| 49 | + public ContainerRequest filter(ContainerRequest cr) { |
| 50 | + String connectorScheme = UriUtils.getHttpConnector().getScheme(); |
| 51 | + if (connectorScheme.equalsIgnoreCase(HTTPS_SCHEME)) { |
| 52 | + URI newUri = cr.getRequestUriBuilder().scheme(HTTPS_SCHEME).build(); |
| 53 | + URI baseUri = cr.getBaseUriBuilder().scheme(HTTPS_SCHEME).build(); |
| 54 | + cr.setUris(baseUri, newUri); |
| 55 | + if (logger.isDebugEnabled()) { |
| 56 | + logger.debug("Detected https connector, rewriting URIs:" + |
| 57 | + newUri + |
| 58 | + "," + |
| 59 | + baseUri); |
| 60 | + } |
| 61 | + } |
| 62 | + return cr; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public ContainerRequestFilter getRequestFilter() { |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public ContainerResponseFilter getResponseFilter() { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments