On Mon, 30 Apr 2001, Craig R. McClanahan wrote:

> 
> 
> On Mon, 30 Apr 2001, Morgan Delagrange wrote:
> 
> > Hi all,
> > 
> > Did we ever reach a consensus on whether or not to use LOG4J?  I just
> > found a 5 page try block that's begging for it.
> > 
> > - Morgan
> > 
> > 
> 
> Use it where?  I don't think very many source code files in Commons are
> five pages long at the moment -- let alone have a five page try/catch
> block.
> 
> Craig
> 

Here's one.  :)  It's from HttpClient.

I guess I don't really care whether or not there is a global "we use
LOG4J" proclamation for Commons, but it seems to make sense in this
case.  There seem to be a fair number of "igore this error and hope
everything is ok" statements that should be captured at a lower debug
level.  HttpClient seems to have a little of its own custom logging stuff,
but I think LOG4J would be more appropriate.

- Morgan


        while ((retries < 5) && (!methodProcessed)) {

            try {

                responseHeaders = null;
                sentRequestBody = false;

                // Send the request header except if the 
                byte[] query = sendRequestHeader(method);

                if ((!http11) || (!method.needExpectation())) {
                    sendRequestBody(method, query);
                    sentRequestBody = true;
                }

                if (connectionInterceptor != null) {
                    connectionInterceptor.sentRequest();
                }
                
                boolean closeOutput = needToCloseOutput();
                if (closeOutput) {
                    try {
                        Class[] paramsClasses = new Class[0];
                        Method shutdownOutput =
socket.getClass().getMethod
                            ("shutdownOutput", paramsClasses);
                        Object[] params = new Object[0];
                        shutdownOutput.invoke(socket, params);
                    } catch (Exception e) {
                        // Ignore, and hope everything goes right
                    }
                }

                // Parsing response

                // Parse status line
                String statusLine = readLine(input);
                if (statusLine == null)
                    throw new IOException("Couldn't parse status line");
                parseStatusLine(statusLine, method);

                // Parse headers
                responseHeaders = parseHeaders(input);

                while (method.getStatusCode() < 200) {
                    
                    if (connectionInterceptor != null) {
                        connectionInterceptor.info
                            (method.getStatusCode(), responseHeaders);
                    }
                    
                    if (method.getStatusCode() ==
HttpStatus.SC_CONTINUE) {
                        if (!sentRequestBody) {
                            if (connectionInterceptor != null) {

connectionInterceptor.recievedExpectation();
                            }
                            sendRequestBody(method, query);
                            sentRequestBody = true;
                        }
                    }
                    
                    statusLine = readLine(input);
                    if (statusLine == null)
                        throw new IOException("Couldn't parse status
line");
                    parseStatusLine(statusLine, method);
                    
                    // Parse headers
                    responseHeaders = parseHeaders(input);
                    
                }

                if (connectionInterceptor != null) {
                    connectionInterceptor.recievedResponse();
                }
                
                // Retrieve the authenticate challenge, if any
                // (needed in case of a digest challenge, for which the
                // header is not constant)
                Header authenticateChallenge =
                    (Header) responseHeaders.get("www-authenticate");
                if (authenticateChallenge != null) {
                    state.setAuthenticateToken
                        (authenticateChallenge.getValue());
                    if (connectionInterceptor != null) {
                        connectionInterceptor.authenticate();
                    }
                }
                
                if ((method.getStatusCode()
                     == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (method.getStatusCode()
                        == HttpStatus.SC_MOVED_PERMANENTLY)) {
                    if (method.followRedirects()) {
                        // Retrieve the location header
                        // NOTE : Redirects across servers are not
                        // supported yet (and perhaps will never be for
                        // security reasons)
                        Header location =
                            (Header) responseHeaders.get("location");
                        if (location != null) {
                            String absolutePath = location.getValue();
                            if (absolutePath.startsWith("http://";)) {
                                absolutePath = absolutePath.substring(7);
                            }
                            int slash = absolutePath.indexOf('/');
                            if (slash != -1) {
                                absolutePath =
absolutePath.substring(slash);
                            }
                            if (absolutePath.equals("")) {
                                absolutePath = "/";
                            }
                            method.setPath(absolutePath);
                        }
                    } else {
                        methodProcessed = true;
                    }
                }

                if ( (method.getStatusCode() !=
HttpStatus.SC_UNAUTHORIZED)
                     && (method.getStatusCode()
                         != HttpStatus.SC_MOVED_TEMPORARILY)
                     && (method.getStatusCode()
                         != HttpStatus.SC_MOVED_PERMANENTLY)
                     && (method.getStatusCode()
                         != HttpStatus.SC_CONTINUE)) {
                    methodProcessed = true;
                } else {
                    if (!methodProcessed) {

                        if (connectionInterceptor != null) {
                            connectionInterceptor.retry
                                (method.getStatusCode());
                        }

                        // Consume bytes returned (if any)
                        method.processResponseHeaders(responseHeaders);
                        ResponseInputStream responseInputStream =
                            new ResponseInputStream(input,
responseHeaders);
                        // FIXME : Really set the interceptors here ?
                        // The content is meant to be discarded
                        //responseInputStream.setInterceptor
                        //    (streamInterceptor);
                        responseInputStream.close();
                        if (closeOutput ||
                            needToCloseConnection(method,
responseHeaders)) {
                            // Disconnect and reconnect if needed
                            closeConnection();
                            openConnection();
                        }
                    }
                }

            } catch (IOException e) {
                if (connectionInterceptor != null) {
                    connectionInterceptor.error(method.getStatusCode(),
e);
                }
                if (debug > 1)
                    e.printStackTrace();
                // If something goes wrong, disconnect, then reconnect
                try {
                    closeConnection();
                } catch (IOException ex) {
                    // Silent catch
                }
                openConnection();
            } catch (HttpException e) {
                if (connectionInterceptor != null) {
                    connectionInterceptor.error(e.getStatusCode(), e);
                }
                // During communication, save the status code.
                if (e.getStatusCode() > 0)
                    method.setStatusCode(e.getStatusCode());
                if (debug > 1)
                    e.printStackTrace();
                // If something goes wrong, disconnect, then reconnect
                try {
                    closeConnection();
                } catch (IOException ex) {
                    // Silent catch
                }
                openConnection();
            }

            retries++;

        }

Reply via email to