use of javax.servlet.http.HttpServletResponseWrapper in project lucene-solr by apache.
the class KerberosPlugin method doAuthenticate.
@Override
public boolean doAuthenticate(ServletRequest req, ServletResponse rsp, FilterChain chain) throws Exception {
log.debug("Request to authenticate using kerberos: " + req);
final HttpServletResponse frsp = (HttpServletResponse) rsp;
// kerberosFilter may close the stream and write to closed streams,
// see HADOOP-13346. To work around, pass a PrintWriter that ignores
// closes
HttpServletResponse rspCloseShield = new HttpServletResponseWrapper(frsp) {
@SuppressForbidden(reason = "Hadoop DelegationTokenAuthenticationFilter uses response writer, this" + "is providing a CloseShield on top of that")
@Override
public PrintWriter getWriter() throws IOException {
final PrintWriter pw = new PrintWriterWrapper(frsp.getWriter()) {
@Override
public void close() {
}
;
};
return pw;
}
};
kerberosFilter.doFilter(req, rspCloseShield, chain);
String requestContinuesAttr = (String) req.getAttribute(RequestContinuesRecorderAuthenticationHandler.REQUEST_CONTINUES_ATTR);
if (requestContinuesAttr == null) {
log.warn("Could not find " + RequestContinuesRecorderAuthenticationHandler.REQUEST_CONTINUES_ATTR);
return false;
} else {
return Boolean.parseBoolean(requestContinuesAttr);
}
}
use of javax.servlet.http.HttpServletResponseWrapper in project sling by apache.
the class ExternalServletContextWrapperTest method testUnwrappingSlingResponse.
/**
* Unwrapping a sling response should return the first-level response wrapped
* by the sling response.
*/
@Test
public void testUnwrappingSlingResponse() {
final HttpServletResponse resp = context.mock(HttpServletResponse.class);
final HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(resp);
final HttpServletResponseWrapper wrapper2 = new HttpServletResponseWrapper(wrapper);
final SlingHttpServletResponseImpl slingResponse = new SlingHttpServletResponseImpl(null, wrapper2);
ServletResponse unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletResponse(slingResponse);
assertEquals(wrapper2, unwrapped);
}
use of javax.servlet.http.HttpServletResponseWrapper in project sling by apache.
the class ExternalServletContextWrapperTest method testUnwrappingWrappedSlingResponse.
/**
* Unwrapping a wrapped sling response should return the first-level response
* wrapped by the sling response.
*/
@Test
public void testUnwrappingWrappedSlingResponse() {
final HttpServletResponse resp = context.mock(HttpServletResponse.class);
final HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(resp);
final HttpServletResponseWrapper wrapper2 = new HttpServletResponseWrapper(wrapper);
final SlingHttpServletResponseImpl slingResponse = new SlingHttpServletResponseImpl(null, wrapper2);
final HttpServletResponseWrapper slingWrapper = new HttpServletResponseWrapper(slingResponse);
ServletResponse unwrapped = ExternalServletContextWrapper.RequestDispatcherWrapper.unwrapServletResponse(slingWrapper);
assertEquals(wrapper2, unwrapped);
}
use of javax.servlet.http.HttpServletResponseWrapper in project stanbol by apache.
the class AuthenticatingFilter method doFilter.
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain chain) throws IOException, ServletException {
logger.debug("filtering request");
final HttpServletRequest request = (HttpServletRequest) servletRequest;
final HttpServletResponse response = (HttpServletResponse) servletResponse;
final Subject subject = getSubject();
{
AuthenticationMethod authenticationMethod = null;
try {
for (Iterator<WeightedAuthenticationMethod> it = methodList.iterator(); it.hasNext(); ) {
authenticationMethod = it.next();
if (authenticationMethod.authenticate(request, subject)) {
break;
}
}
} catch (LoginException ex) {
if (!authenticationMethod.writeLoginResponse(request, response, ex)) {
writeLoginResponse(request, response, ex);
}
return;
}
}
Set<Principal> principals = subject.getPrincipals();
if (principals.size() == 0) {
principals.add(UserUtil.ANONYMOUS);
}
//the response wrapping is because of JERSEY-1926
final ServletOutputStream[] out = new ServletOutputStream[1];
final boolean[] closed = new boolean[1];
final String[] sentErrorMsg = new String[1];
final int[] sentErrorCode = new int[1];
sentErrorCode[0] = -1;
try {
Subject.doAsPrivileged(subject, new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
HttpServletResponse wrapped = new HttpServletResponseWrapper(response) {
@Override
public ServletOutputStream getOutputStream() throws IOException {
final ServletOutputStream orig = response.getOutputStream();
out[0] = orig;
return new ServletOutputStream() {
@Override
public void write(int i) throws IOException {
orig.write(i);
}
@Override
public void close() throws IOException {
closed[0] = true;
}
};
}
@Override
public void sendError(int sc, String msg) throws IOException {
sentErrorCode[0] = sc;
sentErrorMsg[0] = msg;
}
};
chain.doFilter(request, wrapped);
return null;
}
}, null);
} catch (PrivilegedActionException e) {
Throwable cause = e.getCause();
if (cause instanceof ServletException) {
if (cause.getCause() instanceof SecurityException) {
//working around JERSEY-1926
writeLoginResponse(request, response, cause.getCause());
} else {
throw (ServletException) cause;
}
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new RuntimeException(e);
} catch (SecurityException e) {
writeLoginResponse(request, response, e);
}
if (!response.isCommitted()) {
if (closed[0]) {
try {
out[0].close();
} catch (IOException e) {
//do nothing
}
}
if (sentErrorCode[0] > -1) {
response.sendError(sentErrorCode[0], sentErrorMsg[0]);
}
}
}
Aggregations