/*
* Copyright 2002-2007 the original author or authors.
*
* 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 org.internna.iwebmvc.spring.services.dwr.impl;
import org.internna.iwebmvc.spring.services.dwr.*;
import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.io.FileTransfer;
import org.internna.iwebmvc.core.IWebMvcException;
import org.internna.iwebmvc.core.crypto.Cipher;
import org.internna.iwebmvc.core.dao.DAO;
import org.internna.iwebmvc.model.DocumentHolder;
import org.internna.iwebmvc.model.core.Document;
import org.internna.iwebmvc.model.core.TemporalFile;
import org.internna.iwebmvc.repository.DocumentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
/**
* Uploads documents and monitors the progress.
*
* @author Jose Noheda
* @since 1.0
*/
@RemoteProxy(name = "UploadMonitor")
public class UploadMonitorImpl implements UploadMonitor {
@Autowired protected DocumentRepository documentRepository;
@Autowired @Qualifier("baseDAO") protected DAO dao;
/**
* Returns the status of an upload in progress.
* @return a status bean
*/
@Override
@RemoteMethod
public Object getStatus(HttpServletRequest request) {
return request.getSession().getAttribute("PROGRESS_LISTENER");
}
/**
* Cancels an upload in progress.
*/
@Override
@RemoteMethod
public void cancelUpload(){
WebContextFactory.get().getSession().setAttribute("CANCEL_UPLOAD", true);
}
/**
* Uploads a document using DWR. Saves a TemporalFile record the upload
* can be sweeped if the actual Document is not persisted.
*
* @param file the transfer
* @return
*/
@Cipher
@Override
@RemoteMethod
public Document upload(FileTransfer file) {
DocumentHolder holder = new DocumentHolder();
holder.setStream(file.getInputStream());
holder.setMimeType(file.getMimeType());
holder.setIdentifier(file.getName());
holder.setImageProperties();
documentRepository.store(holder);
try {
dao.create(new TemporalFile(holder.getUri()));
} catch (Exception e) {
documentRepository.delete(holder.getUri());
throw new IWebMvcException("Could not create temporal file", e);
}
return holder;
}
}