Example usage for android.util Log println

List of usage examples for android.util Log println

Introduction

In this page you can find the example usage for android.util Log println.

Prototype

public static int println(int priority, String tag, String msg) 

Source Link

Document

Low-level logging call.

Usage

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

private Header getAuthHeader() {
    Header selectedAuthHeader = null;//from  www.j  a va 2  s. com
    for (Header h : responseHeaders) {
        if (debugThisRequest)
            Log.println(Constants.LOGV, TAG, "Looking for auth in Header: " + h.getName() + ":" + h.getValue());
        if (h.getName().equalsIgnoreCase("WWW-Authenticate")) {
            // If this is a digest Auth header we will return with it
            for (HeaderElement he : h.getElements()) {

                if (he.getName().substring(0, 7).equalsIgnoreCase("Digest ")) {
                    return h;
                } else if (he.getName().substring(0, 6).equalsIgnoreCase("Basic ")) {
                    if (selectedAuthHeader == null)
                        selectedAuthHeader = h;
                }
            }
        }
    }
    return selectedAuthHeader;
}

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

private void logEntityLines(int logLevel, String prefix, String entityString) {
    for (String line : entityString.toString().split("\n")) {
        if (line.length() == entityString.toString().length()) {
            int end;
            int length = line.length();
            for (int pos = 0; pos < length; pos += LONG_LINE_WRAP_FOR_DEBUG) {
                end = pos + LONG_LINE_WRAP_FOR_DEBUG;
                if (end > length)
                    end = length;//w w w  . ja  v  a2  s  .  c om
                Log.println(logLevel, TAG, prefix + line.substring(pos, end));
            }
        } else {
            Log.println(logLevel, TAG, prefix + line.replaceAll("\r$", ""));
        }
    }
}

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

/**
 * Log the full details of the request./*from  ww  w  .  j  av  a2  s. co m*/
 * @param logLevel
 */
public void logRequest(int logLevel) {
    Log.println(logLevel, TAG, method + " " + this.fullUrl());
    if (request == null) {
        Log.w(TAG, "Attempting to log request entity but request is null!");
        return;
    }

    for (Header h : request.getAllHeaders()) {
        Log.println(logLevel, TAG, "H>  " + h.getName() + ":" + h.getValue());
    }
    if (request.getEntity() == null)
        return;

    String entityString = entityToString(request.getEntity());
    if (entityString != null) {
        Log.println(logLevel, TAG, "----------------------- vvv Request Body vvv -----------------------");
        logEntityLines(logLevel, "R>  ", entityString);
        Log.println(logLevel, TAG, "----------------------- ^^^ Request Body ^^^ -----------------------");
    }
}

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

public InputStream logResponse(int logLevel) {
    if (response == null) {
        Log.w(TAG, "Attempting to log response entity but response is null!");
        return null;
    }// w  w  w .ja va2  s.  c  o  m
    Log.println(logLevel, TAG, "RESPONSE: " + response.getStatusLine().getProtocolVersion() + " "
            + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());

    for (Header h : responseHeaders) {
        Log.println(logLevel, TAG, "H<  " + h.getName() + ": " + h.getValue());
    }

    if (response.getEntity() == null) {
        Log.println(logLevel, TAG, "Attempting to log response entity but response.getEntity() is null :-(");
        return null;
    }

    String entityString = entityToString(response.getEntity());
    if (entityString != null) {
        Log.println(logLevel, TAG, "----------------------- vvv Response Body vvv -----------------------");
        logEntityLines(logLevel, "R<  ", entityString);
        Log.println(logLevel, TAG, "----------------------- ^^^ Response Body ^^^ -----------------------");
    }
    return new ByteArrayInputStream(entityString.getBytes());
}

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

/**
 * Marshall and send the request./*w  ww  . j  ava 2  s . c om*/
 * @param headers
 * @param entityString
 * @return
 * @throws SendRequestFailedException
 * @throws SSLException
 * @throws AuthenticationFailure
 * @throws ConnectionFailedException
 * @throws ConnectionPoolTimeoutException
 */
private synchronized InputStream sendRequest(Header[] headers, String entityString)
        throws SendRequestFailedException, SSLException, AuthenticationFailure, ConnectionFailedException,
        ConnectionPoolTimeoutException {
    long down = 0;
    long up = 0;
    long start = System.currentTimeMillis();

    if (!initialised)
        throw new IllegalStateException("AcalRequestor has not been initialised!");
    statusCode = -1;
    try {
        // Create request and add headers and entity
        request = new DavRequest(method, this.fullUrl());
        //         request.addHeader(new BasicHeader("User-Agent", AcalConnectionPool.getUserAgent()));
        if (headers != null)
            for (Header h : headers)
                request.addHeader(h);

        if (authRequired && authType != Servers.AUTH_NONE)
            request.addHeader(buildAuthHeader());
        else if (authRequired) {
            // Assume basicAuth
            request.addHeader(basicAuthHeader());
        }

        if (entityString != null) {
            request.setEntity(new StringEntity(entityString.toString(), "UTF-8"));
            up = request.getEntity().getContentLength();
        }

        // This trick greatly reduces the occurrence of host not found errors.
        try {
            InetAddress.getByName(this.hostName);
        } catch (UnknownHostException e1) {
            Thread.sleep(100);
            try {
                InetAddress.getByName(this.hostName);
            } catch (UnknownHostException e2) {
                Thread.sleep(100);
            }
        }

        int requestPort = -1;
        if (this.protocol == null)
            this.protocol = PROTOCOL_HTTP;
        String requestProtocol = this.protocol;
        if ((this.protocol.equals(PROTOCOL_HTTP) && this.port != 80)
                || (this.protocol.equals(PROTOCOL_HTTPS) && this.port != 443)) {
            requestPort = this.port;
        }

        if (Constants.LOG_DEBUG || debugThisRequest) {
            Log.println(Constants.LOGD, TAG,
                    String.format("Method: %s, Protocol: %s, Hostname: %s, Port: %d, Path: %s", method,
                            requestProtocol, hostName, requestPort, path));
        }
        HttpHost host = new HttpHost(this.hostName, requestPort, requestProtocol);

        if (debugThisRequest)
            logRequest(Constants.LOGV);

        // Send request and get response
        response = null;

        if (Constants.debugHeap)
            AcalDebug.heapDebug(TAG, "Making HTTP request");
        try {
            response = httpClient.execute(host, request);
        } catch (ConnectionPoolTimeoutException e) {
            Log.println(Constants.LOGI, TAG,
                    e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
            Log.println(Constants.LOGI, TAG, "Retrying...");
            response = httpClient.execute(host, request);
        }
        if (Constants.debugHeap)
            AcalDebug.heapDebug(TAG, "Finished HTTP request");

        this.responseHeaders = response.getAllHeaders();
        this.statusCode = response.getStatusLine().getStatusCode();

        HttpEntity entity = response.getEntity();
        down = (entity == null ? 0 : entity.getContentLength());

        long finish = System.currentTimeMillis();
        double timeTaken = (finish - start) / 1000.0;

        if (Constants.LOG_DEBUG || debugThisRequest)
            Log.println(Constants.LOGD, TAG, "Response: " + statusCode + ", Sent: " + up + ", Received: " + down
                    + ", Took: " + timeTaken + " seconds");

        if (debugThisRequest) {
            return logResponse(Constants.LOGV);
        } else if (entity != null) {
            if (entity.getContentLength() > 0)
                return entity.getContent();

            // Kind of admitting defeat here, but I can't track down why we seem
            // to end up in never-never land if we just return entity.getContent()
            // directly when entity.getContentLength() is -1 ('unknown', apparently).
            // Horribly inefficient too.
            //
            // @todo: Check whether this problem was caused by failing to close the InputStream
            // and this hack can be removed...  Need to find a server which does not send Content-Length headers.
            //
            String tmpEntity = entityToString(entity);
            return new ByteArrayInputStream(tmpEntity.getBytes());
        }

    } catch (SSLProtocolException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (SSLHandshakeException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        throw e;
    } catch (SSLException e) {
        if (debugThisRequest)
            Log.println(Constants.LOGD, TAG, Log.getStackTraceString(e));
        throw e;
    } catch (AuthenticationFailure e) {
        if (debugThisRequest)
            Log.println(Constants.LOGD, TAG, Log.getStackTraceString(e));
        throw e;
    } catch (ConnectionPoolTimeoutException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        throw e;
    } catch (SocketTimeoutException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (ConnectTimeoutException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (UnknownHostException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (IOException e) {
        Log.i(TAG, e.getClass().getSimpleName() + ": " + e.getMessage() + " to " + fullUrl());
        return null;
    } catch (Exception e) {
        Log.println(Constants.LOGD, TAG, Log.getStackTraceString(e));
        if (statusCode < 300 || statusCode > 499)
            throw new SendRequestFailedException(e.getMessage());
    }
    return null;
}

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

/**
 * Do a new HTTP <method> request with these headers and entity (request body) against
 * this path (or the current path, if null).  The headers & entity may also be null in
 * some simple cases./*ww  w  . ja va  2  s  .  c  o  m*/
 *
 * If the server requests Digest or Basic authentication a second request will be made
 * supplying these (if possible).  Likewise the method will follow up to five redirects
 * before giving up on a request.
 * @param method
 * @param pathOrUrl
 * @param headers
 * @param entity
 * @return
 * @throws SendRequestFailedException
 * @throws SSLException
 * @throws ConnectionFailedException
 */
public InputStream doRequest(String method, String pathOrUrl, Header[] headers, String entity)
        throws SendRequestFailedException, SSLException, ConnectionFailedException {

    if (Constants.LOG_DEBUG || debugThisRequest)
        Log.println(Constants.LOGD, TAG, String.format("%s request on %s", method, fullUrl()));

    InputStream result = null;
    interpretUriString(pathOrUrl);
    this.method = method;
    do {
        try {
            result = sendRequest(headers, entity);
        } catch (SSLHandshakeException e) {
            throw e;
        } catch (SSLException e) {
            throw e;
        } catch (SendRequestFailedException e) {
            throw e;
        } catch (ConnectionFailedException e) {
            throw e;
        } catch (AuthenticationFailure e1) {
            statusCode = 401;
        } catch (Exception e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }

        if (statusCode == 401) {
            // In this case we didn't send auth credentials the first time, so
            // we need to try again after we interpret the auth request.
            try {
                interpretRequestedAuth(getAuthHeader());
                return sendRequest(headers, entity);
            } catch (AuthenticationFailure e1) {
                throw new SendRequestFailedException("Authentication Failed: " + e1.getMessage());
            } catch (Exception e) {
                Log.e(TAG, Log.getStackTraceString(e));
            }
        }

        if ((statusCode >= 300 && statusCode <= 303) || statusCode == 307) {
            /**
             * Other than 301/302 these are all pretty unlikely
             *      300:  Multiple choices, but we take the one in the Location header anyway
             *      301:  Moved permanently
             *      302:  Found (was 'temporary redirect' once in prehistory)
             *      303:  See other
             *      307:  Temporary redirect. Meh.
             */
            if (redirectCount++ < redirectLimit) {
                String oldUrl = fullUrl();
                interpretUriString(getLocationHeader());
                if (debugThisRequest)
                    Log.println(Constants.LOGD, TAG, method + " " + oldUrl + " redirected to: " + fullUrl());

                continue;
            }
        } else
            break;
    } while (redirectCount < redirectLimit);

    return result;
}

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

/**
 * <p>/*from ww w  .  j a  va2s.  c o m*/
 * Does an XML request against the specified path (or the previously set path, if null),
 * following redirects and returning the root DavNode of an XML tree.
 * </p>
 *
 * @return <p>
 *         A DavNode which is the root of the multistatus response, or null if it couldn't be parsed.
 *         </p>
 * @throws SSLHandshakeException
 */
public DavNode doXmlRequest(String method, String requestPath, Header[] headers, String xml)
        throws SSLHandshakeException {
    long start = System.currentTimeMillis();

    InputStream responseStream = null;
    DavNode root = null;
    try {
        responseStream = doRequest(method, requestPath, headers, xml);
        if (responseHeaders == null) {
            return root;
        }
        for (Header h : responseHeaders) {
            if ("Content-Type".equals(h.getName())) {
                for (HeaderElement he : h.getElements()) {
                    if ("text/plain".equals(he.getName()) || "text/html".equals(he.getName())) {
                        Log.println(Constants.LOGI, TAG, "Response is not an XML document");
                        if (responseStream != null)
                            responseStream.close();
                        return root;
                    }
                }
            }
        }
        if (statusCode == 404 || statusCode == 401) {
            return root;
        }
        root = DavParserFactory.buildTreeFromXml(Constants.XMLParseMethod, responseStream);
    } catch (SSLHandshakeException e) {
        throw e;
    } catch (Exception e) {
        Log.i(TAG, e.getMessage(), e);
        return null;
    } finally {
        if (responseStream != null)
            try {
                responseStream.close();
            } catch (IOException e) {
            }
    }

    if (debugThisRequest)
        Log.println(Constants.LOGV, TAG,
                "Request and parse completed in " + (System.currentTimeMillis() - start) + "ms");
    return root;
}