/** * Returns the final response from the servlet. Note that this method should * only be invoked after all processing has been done to the servlet response. **/ public WebResponse getServletResponse() throws IOException { if (_contextStack.size() != 1) throw new IllegalStateException( "Have not returned from all request dispatchers" ); if (_webResponse == null) { HttpSession session = getRequest().getSession( /* create */ false ); if (session != null && session.isNew()) { Cookie cookie = new Cookie( ServletUnitHttpSession.SESSION_COOKIE_NAME, session.getId() ); cookie.setPath( _application.getContextPath() ); getResponse().addCookie( cookie ); } _webResponse = new ServletUnitWebResponse( _client, _frame, _effectiveURL, getResponse(), _client.getExceptionsThrownOnErrorStatus() ); } return _webResponse; }
boolean needToAuthenticate() { if (getAuthenticationType() == null) return false; if (getCredentialsForRealm() != null) return true; if (!_client.getExceptionsThrownOnErrorStatus()) return false; throw createAuthorizationRequiredException(); }
boolean needToAuthenticate() { if (getAuthenticationType() == null) return false; if (getCredentialsForRealm() != null) return true; if (!_client.getExceptionsThrownOnErrorStatus()) return false; throw createAuthorizationRequiredException(); }
boolean needToAuthenticate() { if (getAuthenticationType() == null) return false; if (getCredentialsForRealm() != null) return true; if (!_client.getExceptionsThrownOnErrorStatus()) return false; throw createAuthorizationRequiredException(); }
/** * Creates a web response object which represents the response to the specified web request. **/ protected WebResponse newResponse( WebRequest request, FrameSelector targetFrame ) throws MalformedURLException, IOException { Properties savedProperties = (Properties) System.getProperties().clone(); try { if (_proxyHost != null) { System.setProperty( "proxyHost", _proxyHost ); System.setProperty( "proxyPort", Integer.toString( _proxyPort ) ); } URLConnection connection = openConnection( getRequestURL( request ) ); // [ 1518901 ] enable http connect and read timeouts (needs JDK 1.5) // XXX enable for 1.7 release in 2008 // comment out if you need this and have JDK 1.5 // if (_connectTimeout>=0) connection.setConnectTimeout( _connectTimeout ); // if (_readTimeout>=0) connection.setReadTimeout( _readTimeout ); if (HttpUnitOptions.isLoggingHttpHeaders()) { String urlString = request.getURLString(); System.out.println( "\nConnecting to " + request.getURL().getHost() ); System.out.println( "Sending:: " + request.getMethod() + " " + urlString ); } sendHeaders( connection, getHeaderFields( request.getURL() ) ); sendHeaders( connection, request.getHeaderDictionary() ); request.completeRequest( connection ); return new HttpWebResponse( this, targetFrame, request, connection, getExceptionsThrownOnErrorStatus() ); } finally { System.setProperties( savedProperties ); } }
/** * Sends a request and returns a response after dealing with any authentication challenge. If challenged and able * to respond, resends the request after setting the authentication header (which will apply only for the that request). * @param request the original request * @param targetFrame the frame into which the result will be stored * @return a response from the server * @throws IOException if an exception (including authorization failure) occurs */ WebResponse createResponse( WebRequest request, FrameSelector targetFrame ) throws IOException { WebResponse response = newResponse( request, targetFrame ); AuthenticationChallenge challenge = new AuthenticationChallenge( this, request, response.getHeaderField( "WWW-Authenticate" ) ); if (!challenge.needToAuthenticate()) { return response; } else { setOnetimeAuthenticationHeader( challenge.createAuthenticationHeader() ); WebResponse response2 = newResponse( request, targetFrame ); if (response2.getHeaderField( "WWW-Authenticate" ) != null && getExceptionsThrownOnErrorStatus()) { throw AuthenticationChallenge.createException( response2.getHeaderField( "WWW-Authenticate" ) ); } return response2; } }
/** * Sends a request and returns a response after dealing with any authentication challenge. If challenged and able * to respond, resends the request after setting the authentication header (which will apply only for the that request). * @param request the original request * @param targetFrame the frame into which the result will be stored * @return a response from the server * @throws IOException if an exception (including authorization failure) occurs */ WebResponse createResponse( WebRequest request, FrameSelector targetFrame ) throws IOException { WebResponse response = newResponse( request, targetFrame ); AuthenticationChallenge challenge = new AuthenticationChallenge( this, request, response.getHeaderField( "WWW-Authenticate" ) ); if (!challenge.needToAuthenticate()) { return response; } else { setOnetimeAuthenticationHeader( challenge.createAuthenticationHeader() ); WebResponse response2 = newResponse( request, targetFrame ); if (response2.getHeaderField( "WWW-Authenticate" ) != null && getExceptionsThrownOnErrorStatus()) { throw AuthenticationChallenge.createException( response2.getHeaderField( "WWW-Authenticate" ) ); } return response2; } }
/** * Sends a request and returns a response after dealing with any authentication challenge. If challenged and able * to respond, resends the request after setting the authentication header (which will apply only for the that request). * @param request the original request * @param targetFrame the frame into which the result will be stored * @return a response from the server * @throws IOException if an exception (including authorization failure) occurs */ WebResponse createResponse( WebRequest request, FrameSelector targetFrame ) throws IOException { WebResponse response = newResponse( request, targetFrame ); AuthenticationChallenge challenge = new AuthenticationChallenge( this, request, response.getHeaderField( "WWW-Authenticate" ) ); if (!challenge.needToAuthenticate()) { return response; } else { setOnetimeAuthenticationHeader( challenge.createAuthenticationHeader() ); WebResponse response2 = newResponse( request, targetFrame ); if (response2.getHeaderField( "WWW-Authenticate" ) != null && getExceptionsThrownOnErrorStatus()) { throw AuthenticationChallenge.createException( response2.getHeaderField( "WWW-Authenticate" ) ); } return response2; } }
/** * Examines the headers in the response and throws an exception if appropriate. * @parm response - the response to validate **/ private void validateHeaders( WebResponse response ) throws HttpException { if (!getExceptionsThrownOnErrorStatus()) return; // see feature request [ 914314 ] Add HttpException.getResponse for better reporting // for possible improvements here if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { throw new HttpInternalErrorException( response.getURL() ); } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() ); } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() ); } }
/** * Examines the headers in the response and throws an exception if appropriate. * @parm response - the response to validate **/ private void validateHeaders( WebResponse response ) throws HttpException { if (!getExceptionsThrownOnErrorStatus()) return; // see feature request [ 914314 ] Add HttpException.getResponse for better reporting // for possible improvements here if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { throw new HttpInternalErrorException( response.getURL() ); } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() ); } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() ); } }
/** * Examines the headers in the response and throws an exception if appropriate. * @parm response - the response to validate **/ private void validateHeaders( WebResponse response ) throws HttpException { if (!getExceptionsThrownOnErrorStatus()) return; // see feature request [ 914314 ] Add HttpException.getResponse for better reporting // for possible improvements here if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { throw new HttpInternalErrorException( response.getURL() ); } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() ); } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() ); } }