-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
307 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
* Moved jerry.ds.bitarray to jerry.lang | ||
* Moved jerry.ds.refresh to jerry.refresh | ||
* Moved jerry.ds.mutable to jerry.lang | ||
* Moved jerry.ds.bound to jerry.lang | ||
* Remove DumpUtils in favor of GsonUtils.toJson() | ||
* |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/sangupta/jerry/http/TenantDetectionStrategy.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,34 @@ | ||
package com.sangupta.jerry.http; | ||
|
||
/** | ||
* Enumeration for different strategies that help define how the tenant for an | ||
* incoming request needs to be identified. | ||
* | ||
* @author sangupta | ||
* | ||
*/ | ||
public enum TenantDetectionStrategy { | ||
|
||
/** | ||
* HTTP request parameter defines the tenant | ||
*/ | ||
REQUEST_PARAM, | ||
|
||
/** | ||
* HTTP request header defines the tenant | ||
*/ | ||
REQUEST_HTTP_HEADER, | ||
|
||
/** | ||
* A path variable that shall be used to detect the tenant | ||
*/ | ||
PATH_VARIABLE, | ||
|
||
/** | ||
* In this case the user authentication system needs to detect | ||
* and set the tenant based on user's entitlements. | ||
* | ||
*/ | ||
USER_AUTH; | ||
|
||
} |
138 changes: 138 additions & 0 deletions
138
src/main/java/com/sangupta/jerry/http/filter/TenantDetectionServletFilter.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,138 @@ | ||
package com.sangupta.jerry.http.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import com.sangupta.jerry.http.TenantDetectionStrategy; | ||
import com.sangupta.jerry.security.SecurityContext; | ||
import com.sangupta.jerry.util.AssertUtils; | ||
|
||
/** | ||
* Servlet filter that helps identify the incoming tenant. | ||
* | ||
* @author sangupta | ||
* | ||
*/ | ||
public class TenantDetectionServletFilter implements Filter { | ||
|
||
protected TenantDetectionStrategy tenantDetectionStrategy = TenantDetectionStrategy.REQUEST_PARAM; | ||
|
||
protected String tenancyRequestParam = "tenantID"; | ||
|
||
protected String tenancyHttpHeader = "X-Tenant-ID"; | ||
|
||
public void init(FilterConfig filterConfig) throws ServletException { | ||
|
||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
HttpServletRequest hsr = (HttpServletRequest) request; | ||
|
||
// clear existing tenant | ||
SecurityContext.clear(); | ||
|
||
// check if we have a strategy to find out tenant | ||
if (this.tenantDetectionStrategy == null) { | ||
// nothing to do | ||
chain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
// detect tenant based on strategy | ||
switch (this.tenantDetectionStrategy) { | ||
case PATH_VARIABLE: | ||
break; | ||
|
||
case REQUEST_HTTP_HEADER: | ||
this.setTenantFromHttpHeader(hsr); | ||
break; | ||
|
||
case REQUEST_PARAM: | ||
this.setTenantFromRequestParam(hsr); | ||
break; | ||
|
||
case USER_AUTH: | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
// complete the request | ||
try { | ||
chain.doFilter(request, response); | ||
} finally { | ||
// clean the tenant again | ||
SecurityContext.clear(); | ||
} | ||
} | ||
|
||
/** | ||
* Find the tenant using chosen request http header. | ||
* | ||
* @param hsr the {@link HttpServletRequest} | ||
*/ | ||
protected void setTenantFromHttpHeader(HttpServletRequest hsr) { | ||
String value = hsr.getHeader(this.tenancyHttpHeader); | ||
if (AssertUtils.isEmpty(value)) { | ||
// do nothing | ||
return; | ||
} | ||
|
||
SecurityContext.setTenant(value); | ||
} | ||
|
||
/** | ||
* Find the tenant using chosen request parameter. | ||
* | ||
* @param hsr the {@link HttpServletRequest} | ||
*/ | ||
protected void setTenantFromRequestParam(HttpServletRequest hsr) { | ||
String value = hsr.getParameter(this.tenancyRequestParam); | ||
if (AssertUtils.isEmpty(value)) { | ||
// do nothing | ||
return; | ||
} | ||
|
||
SecurityContext.setTenant(value); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param tenantDetectionStrategy | ||
*/ | ||
public void setTenantDetectionStrategy(TenantDetectionStrategy tenantDetectionStrategy) { | ||
this.tenantDetectionStrategy = tenantDetectionStrategy; | ||
} | ||
|
||
/** | ||
* | ||
* @param tenancyHttpHeader | ||
*/ | ||
public void setTenancyHttpHeader(String tenancyHttpHeader) { | ||
this.tenancyHttpHeader = tenancyHttpHeader; | ||
} | ||
|
||
/** | ||
* | ||
* @param tenancyRequestParam | ||
*/ | ||
public void setTenancyRequestParam(String tenancyRequestParam) { | ||
this.tenancyRequestParam = tenancyRequestParam; | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.