Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * 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.
  */
 package org.apache.sling.engine.impl.log;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 
 
     // the content type header name
     private static final String HEADER_CONTENT_TYPE = "Content-Type";
 
     // the content length header name
     private static final String HEADER_CONTENT_LENGTH = "Content-Length";

    
format for RFC 1123 date string -- "Sun, 06 Nov 1994 08:49:37 GMT"
 
     private final static SimpleDateFormat RFC1123_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
         .);

    
The counter for request gone through this filter. As this is the first request level filter hit, this counter should actually count each request which at least enters the request level component filter processing.

This counter is reset to zero, when this component is activated. That is, each time this component is restarted (system start, bundle start, reconfiguration), the request counter restarts at zero.

 
     private static AtomicLong requestCounter = new AtomicLong();
 
     // TODO: more content related headers, namely Content-Language should
     // probably be supported
 
     // the request counter
     private long requestId;
 
     // the system time in ms when the request entered the system, this is
     // the time this instance was created
     private long requestStart;
 
     // the system time in ms when the request exited the system, this is
     // the time of the call to the requestEnd() method
     private long requestEnd;
 
     // the output stream wrapper providing the transferred byte count
     private LoggerResponseOutputStream out;
 
     // the print writer wrapper providing the transferred character count
     private LoggerResponseWriter writer;
 
     // the caches status
     private int status = ;
 
     // the cookies set during the request, indexed by cookie name
     private Map<StringCookiecookies;
 
     // the headers set during the request, indexed by lower-case header
     // name, value is string for single-valued and list for multi-valued
     // headers
     private Map<StringObjectheaders;
 
         super(response);
 
         this. = .getAndIncrement();
         this. = System.currentTimeMillis();
     }

    
Called to indicate the request processing has ended. This method currently sets the request end time returned by getRequestEnd() and which is used to calculate the request duration.
    void requestEnd() {
        this. = System.currentTimeMillis();
    }
    // ---------- SlingHttpServletResponse interface
    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (this. == null) {
            ServletOutputStream sos = super.getOutputStream();
            this. = new LoggerResponseOutputStream(sos);
        }
        return this.;
    }
    @Override
    public PrintWriter getWriter() throws IOException {
        if (this. == null) {
            PrintWriter pw = super.getWriter();
            this. = new LoggerResponseWriter(pw);
        }
        return this.;
    }
    // ---------- Error handling through Sling Error Resolver -----------------
    @Override
    public void sendRedirect(String locationthrows IOException {
        super.sendRedirect(location);
        // replicate the status code of call to base class
        this. = ;
    }
    @Override
    public void sendError(int statusthrows IOException {
        super.sendError(status);
        this. = status;
    }
    @Override
    public void sendError(int statusString messagethrows IOException {
        super.sendError(statusmessage);
        this. = status;
    }
    @Override
    public void setStatus(int statusString message) {
        super.setStatus(statusmessage);
        this. = status;
    }
    @Override
    public void setStatus(int status) {
        super.setStatus(status);
        this. = status;
    }
    @Override
    public void addCookie(Cookie cookie) {
        // register the cookie for later use
        if (this. == null) {
            this. = new HashMap<StringCookie>();
        }
        this..put(cookie.getName(), cookie);
        super.addCookie(cookie);
    }
    @Override
    public void addDateHeader(String namelong date) {
        this.registerHeader(nametoDateString(date), true);
        super.addDateHeader(namedate);
    }
    @Override
    public void addHeader(String nameString value) {
        this.registerHeader(namevaluetrue);
        super.addHeader(namevalue);
    }
    @Override
    public void addIntHeader(String nameint value) {
        this.registerHeader(name, String.valueOf(value), true);
        super.addIntHeader(namevalue);
    }
    @Override
    public void setContentLength(int len) {
        this.registerHeader(, String.valueOf(len), false);
        super.setContentLength(len);
    }
    @Override
    public void setContentType(String type) {
        // SLING-726 No handling required since this seems to be correct
        this.registerHeader(typefalse);
        super.setContentType(type);
    }
    @Override
    public void setCharacterEncoding(String charset) {
        // SLING-726 Ignore call if getWriter() has been called
        if ( == null) {
            super.setCharacterEncoding(charset);
        }
    }
    @Override
    public void setDateHeader(String namelong date) {
        this.registerHeader(nametoDateString(date), false);
        super.setDateHeader(namedate);
    }
    @Override
    public void setHeader(String nameString value) {
        this.registerHeader(namevaluefalse);
        super.setHeader(namevalue);
    }
    @Override
    public void setIntHeader(String nameint value) {
        this.registerHeader(name, String.valueOf(value), false);
        this.setHeader(name, String.valueOf(value));
    }
    @Override
    public void setLocale(Locale loc) {
        // TODO: Might want to register the Content-Language header
        super.setLocale(loc);
    }
    // ---------- Retrieving response information ------------------------------
    public long getRequestId() {
        return this.;
    }
    public long getRequestStart() {
        return this.;
    }
    public long getRequestEnd() {
        return this.;
    }
    public long getRequestDuration() {
        return this. - this.;
    }
    public int getStatus() {
        return this.;
    }
    public int getCount() {
        if (this. != null) {
            return this..getCount();
        } else if (this. != null) {
            return this..getCount();
        }
        // otherwise return zero
        return 0;
    }
    public Cookie getCookie(String name) {
        return (this. != null) ? (Cookiethis..get(name) : null;
    }
    public String getHeadersString(String name) {
        // normalize header name to lower case to support case-insensitive
        // headers
        name = name.toLowerCase();
        Object header = (this. != null) ? this..get(name) : null;
        if (header == null) {
            return null;
        } else if (header instanceof String) {
            return (Stringheader;
        } else {
            StringBuilder headerBuf = new StringBuilder();
            for (Iterator<?> hi = ((List<?>) header).iterator(); hi.hasNext();) {
                if (headerBuf.length() > 0) {
                    headerBuf.append(",");
                }
                headerBuf.append(hi.next());
            }
            return headerBuf.toString();
        }
    }
    // ---------- Internal helper ---------------------------------------------

    
Stores the name header-value pair in the header map. The name is converted to lower-case before using it as an index in the map.

Parameters:
name The name of the header to register
value The value of the header to register
add If true the header value is added to the list of potentially existing header values. Otherwise the new value replaces any existing values.
    @SuppressWarnings("unchecked")
    private void registerHeader(String nameString valueboolean add) {
        // ensure the headers map
        if (this. == null) {
            this. = new HashMap<StringObject>();
        }
        // normalize header name to lower case to support case-insensitive
        // headers
        name = name.toLowerCase();
        // retrieve the current contents if adding, otherwise assume no current
        Object current = add ? this..get(name) : null;
        if (current == null) {
            // set the single value (forced if !add)
            this..put(namevalue);
        } else if (current instanceof String) {
            // create list if a single value is already set
            List<Stringlist = new ArrayList<String>();
            list.add((Stringcurrent);
            list.add(value);
            this..put(namelist);
        } else {
            // append to the list of more than one already set
            ((List<Object>) current).add(value);
        }
    }

    
Converts the time value given as the number of milliseconds since January 1, 1970 to a date and time string compliant with RFC 1123 date specification. The resulting string is compliant with section 3.3.1, Full Date, of RFC 2616 and may thus be used as the value of date header such as Date.

Parameters:
date The date value to convert to a string
Returns:
The string representation of the date and time value.
    public static String toDateString(long date) {
        synchronized () {
            return .format(new Date(date));
        }
    }
    // ---------- byte/character counting output channels ----------------------
    // byte transfer counting ServletOutputStream
    private static class LoggerResponseOutputStream extends ServletOutputStream {
        private ServletOutputStream delegatee;
        private int count;
        LoggerResponseOutputStream(ServletOutputStream delegatee) {
            this. = delegatee;
        }
        public int getCount() {
            return this.;
        }
        @Override
        public void write(int bthrows IOException {
            this..write(b);
            this.++;
        }
        @Override
        public void write(byte[] bthrows IOException {
            this..write(b);
            this. += b.length;
        }
        @Override
        public void write(byte[] bint offint lenthrows IOException {
            this..write(bofflen);
            this. += len;
        }
        @Override
        public void flush() throws IOException {
            this..flush();
        }
        @Override
        public void close() throws IOException {
            this..close();
        }
    }
    // character transfer counting PrintWriter
    private static class LoggerResponseWriter extends PrintWriter {
        private static final int LINE_SEPARATOR_LENGTH = System.getProperty("line.separator").length();
        private int count;
        LoggerResponseWriter(PrintWriter delegatee) {
            super(delegatee);
        }
        public int getCount() {
            return this.;
        }
        @Override
        public void write(int c) {
            super.write(c);
            this.++;
        }
        @Override
        public void write(char[] bufint offint len) {
            super.write(bufofflen);
            this. += len;
        }
        @Override
        public void write(String sint offint len) {
            super.write(sofflen);
            this. += len;
        }
        @Override
        public void println() {
            super.println();
            this. += ;
        }
    }
New to GrepCode? Check out our FAQ X