-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added IP to public host URL redirecting filter
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/erudika/scoold/utils/RedirectingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2013-2024 Erudika. https://erudika.com | ||
* | ||
* Licensed under the EULA - use is subject to license terms. | ||
* | ||
* 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. | ||
* | ||
* For issues and patches go to: https://github.com/erudika/scoold-pro | ||
*/ | ||
package com.erudika.scoold.utils; | ||
|
||
import java.io.IOException; | ||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* | ||
* @author Alex Bogdanovski [[email protected]] | ||
*/ | ||
public class RedirectingFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | ||
HttpServletRequest httpRequest = (HttpServletRequest) req; | ||
HttpServletResponse httpResponse = (HttpServletResponse) res; | ||
String url = httpRequest.getRequestURL().toString(); | ||
url = StringUtils.removeStartIgnoreCase(url, "http://"); | ||
url = StringUtils.removeStartIgnoreCase(url, "https://"); | ||
|
||
if (url.matches("(?:[0-9]{1,3}\\.){3}[0-9]{1,3}(.*)") && ScooldUtils.getConfig().inProduction()) { | ||
String path = StringUtils.substringAfter(url, "/"); | ||
String query = httpRequest.getQueryString(); | ||
httpResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); | ||
httpResponse.setHeader(HttpHeaders.LOCATION, ScooldUtils.getConfig().serverUrl() + | ||
(StringUtils.isBlank(path) ? "" : "/" + path) + | ||
(StringUtils.isBlank(query) ? "" : "?" + query)); | ||
} else { | ||
chain.doFilter(req, res); | ||
} | ||
} | ||
|
||
} |