// Copyright (C) 2007 Google Inc.
//
// Licensed 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 com.google.enterprise.connector.otex;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.enterprise.connector.spi.RepositoryException;
import com.google.enterprise.connector.otex.client.Client;
import com.google.enterprise.connector.otex.client.ClientValue;

/**
 * This content handler implementation uses a Livelink download URL.
 * It depends on the LAPI client, so it can't be used in deployments
 * with the mock client.
 * 
 * @see ContentHandler
 */
class HttpURLContentHandler implements ContentHandler {
    /** The logger for this class. */
    private static final Logger LOGGER =
        Logger.getLogger(HttpURLContentHandler.class.getName());

    /** The connector contains configuration information. */
    private LivelinkConnector connector;

    /** The client provides access to the server. */
    private Client client;

    /** The user's LLCookie value. */
    private String llCookie;

    /** The data URL pattern. */
    String baseUrl = "?func=ll&objAction=download&objId=";  

    HttpURLContentHandler() {
    }

    /** {@inheritDoc} */
    public void initialize(LivelinkConnector connector, Client client)
            throws RepositoryException {
        this.connector = connector;
        this.client = client;

        llCookie = getLLCookie();
        if (LOGGER.isLoggable(Level.FINE))
            LOGGER.fine("LLCOOKIE: " + llCookie);
    }

    /**
     * Sets the baseUrl property. 
     *
     * @param baseUrl the URL pattern for data retrieval; by
     * default "?func=ll&objAction=download&objId="
     * @since 3.0
     */
    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl; 
    }

    /**
     * Gets the LLCookie value for this session.
     *
     * @return the cookie value
     * @throws RepositoryException if the cookie is not set
     */
    private String getLLCookie() throws RepositoryException {
        ClientValue cookies = client.GetCookieInfo();
        for (int i = 0; i < cookies.size(); i++) {
            ClientValue cookie = cookies.toValue(i);
            if ("LLCookie".equalsIgnoreCase(cookie.toString("Name"))) {
                return cookie.toString("Value");
            }
        }
        throw new RepositoryException("Missing LLCookie");
    }
    
    /** {@inheritDoc} */
    public InputStream getInputStream(int volumeId, int objectId,
            int versionNumber, int size) throws RepositoryException {
        try {
            String downloadUrlBase = connector.getDisplayUrl() +
                baseUrl; 
            URL downloadUrl = new URL(downloadUrlBase + objectId);
            if (LOGGER.isLoggable(Level.FINEST))
                LOGGER.fine("DOWNLOAD URL: " + downloadUrl);
            URLConnection download = downloadUrl.openConnection();
            download.addRequestProperty("Cookie", "LLCookie=" + llCookie);

            // XXX: Does the BufferedInputStream help at all?
            return new BufferedInputStream(download.getInputStream(), 32768);
        } catch (IOException e) {
            throw new LivelinkException(e, LOGGER);
        }
    }
}

@@ -1748,48 +1748,153 @@
 The 
-data URL pattern. */%0D%0A    String baseUrl
+Livelink base URL. The default is the configured Livelink URL. */%0D%0A    String urlBase;  %0D%0A%0D%0A    /** The document URL path. */%0D%0A    String urlPath
  = %22
@@ -2232,147 +2232,420 @@
     
-if (LOGGER.isLoggable(Level.FINE))
+urlBase = connector.getDisplayUrl();%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Sets the document base URL. 
 %0D%0A     
+*%0D%0A
      
-  LOGGER.fine(%22LLCOOKIE: %22 + llCookie);%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Sets the baseUrl property. 
+* @param urlBase the Livelink base URL for document retrieval; by%0D%0A     * default the configured Livelink URL%0D%0A     * @since 3.0%0D%0A     */%0D%0A    public void setUrlBase(String urlBase) %7B%0D%0A        this.urlBase = urlBase; %0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Sets the document URL path, which is appended to the base URL.
 %0D%0A  
@@ -2664,23 +2664,23 @@
  @param 
-baseUrl
+urlPath
  the URL
@@ -2687,21 +2687,22 @@
  pat
-tern
+h
  for d
-ata
+ocument
  ret
@@ -2817,23 +2817,23 @@
  set
-BaseUrl
+UrlPath
 (String 
 base
@@ -2828,23 +2828,23 @@
 (String 
-baseUrl
+urlPath
 ) %7B%0D%0A   
@@ -2857,25 +2857,25 @@
 his.
-baseUrl = baseUrl
+urlPath = urlPath
 ; %0D%0A
@@ -3682,101 +3682,8 @@
  %7B%0D%0A
-            String downloadUrlBase = connector.getDisplayUrl() +%0D%0A                baseUrl; %0D%0A
     
@@ -3720,23 +3720,25 @@
 URL(
-downloadUrlBase
+urlBase + urlPath
  + o

Make the Livelink base URL used by HttpURLContentHandler configurable.