/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.repo.content.http;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.error.AlfrescoRuntimeException;
import org.springframework.extensions.surf.util.I18NUtil;
import org.alfresco.repo.content.AbstractContentReader;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentStreamListener;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.transaction.TransactionService;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* The reader that does the actual communication with the Alfresco HTTP
* application.
*
* @see HttpAlfrescoStore
* @since 2.1
* @author Derek Hulley
*/
public class HttpAlfrescoContentReader extends AbstractContentReader
{
private static final String ERR_NO_CONNECTION = "content.http_reader.err.no_connection";
private static final String ERR_NO_AUTHENTICATION = "content.http_reader.err.no_authentication";
private static final String ERR_CHECK_CLUSTER = "content.http_reader.err.check_cluster";
private static final String ERR_UNRECOGNIZED = "content.http_reader.err.unrecognized";
private static final String DEFAULT_URL = "{0}/dr?contentUrl={1}&ticket={2}";
private static final String INFO_ONLY = "&infoOnly=true";
private static Log logger = LogFactory.getLog(HttpAlfrescoContentReader.class);
private TransactionService transactionService;
private AuthenticationService authenticationService;
private String baseHttpUrl;
// Helpers
private HttpClient httpClient;
private PropagateTicketCallback ticketCallback;
// Cached values
private boolean isInfoCached;
private boolean cachedExists;
private long cachedLastModified;
private long cachedSize;
public HttpAlfrescoContentReader(
TransactionService transactionService,
AuthenticationService authenticationService,
String baseHttpUrl,
String contentUrl)
{
super(contentUrl);
this.transactionService = transactionService;
this.authenticationService = authenticationService;
this.baseHttpUrl = baseHttpUrl;
// Helpers
this.httpClient = new HttpClient();
this.ticketCallback = new PropagateTicketCallback();
// A trip to the remote server has not been made
cachedExists = false;
cachedSize = 0L;
cachedLastModified = 0L;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(100);
sb.append("HttpAlfrescoContentReader")
.append("[ contentUrl=").append(getContentUrl())
.append("]");
return sb.toString();
}
/**
* Helper class to wrap the ticket creation in order to force propagation of the
* authentication ticket around the cluster.
*
* @since 2.1
* @author Derek Hulley
*/
private class PropagateTicketCallback implements RetryingTransactionCallback
{
public String execute() throws Throwable
{
return authenticationService.getCurrentTicket();
}
}
private void getInfo()
{
RunAsWork