Java tutorial
/* * Copyright (C) 2012 McEvoy Software Ltd * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package com.ettrema.http.fs; import com.bradmcevoy.common.ContentTypeUtils; import com.bradmcevoy.http.Auth; import com.bradmcevoy.http.CopyableResource; import com.bradmcevoy.http.DeletableResource; import com.bradmcevoy.http.GetableResource; import com.bradmcevoy.http.MoveableResource; import com.bradmcevoy.http.PropFindableResource; import com.bradmcevoy.http.PropPatchableResource; import com.bradmcevoy.http.Range; import com.bradmcevoy.http.Request; import com.bradmcevoy.http.entity.PartialEntity; import com.bradmcevoy.http.exceptions.NotFoundException; import com.bradmcevoy.http.webdav.PropPatchHandler.Fields; import com.bradmcevoy.io.ReadingException; import com.bradmcevoy.io.WritingException; import java.io.*; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * */ public class FsFileResource extends FsResource implements CopyableResource, DeletableResource, GetableResource, MoveableResource, PropFindableResource, PropPatchableResource { private static final Logger log = LoggerFactory.getLogger(FsFileResource.class); private final FileContentService contentService; /** * * @param host - the requested host. E.g. www.mycompany.com * @param factory * @param file */ public FsFileResource(String host, FileSystemResourceFactory factory, File file, FileContentService contentService) { super(host, factory, file); this.contentService = contentService; } @Override public Long getContentLength() { return file.length(); } @Override public String getContentType(String preferredList) { String mime = ContentTypeUtils.findContentTypes(this.file); String s = ContentTypeUtils.findAcceptableContentType(mime, preferredList); if (log.isTraceEnabled()) { log.trace("getContentType: preferred: {} mime: {} selected: {}", new Object[] { preferredList, mime, s }); } return s; } @Override public String checkRedirect(Request arg0) { return null; } @Override public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotFoundException { InputStream in = null; try { in = contentService.getFileContent(file); if (range != null) { log.debug("sendContent: ranged content: " + file.getAbsolutePath()); PartialEntity.writeRange(in, range, out); } else { log.debug("sendContent: send whole file " + file.getAbsolutePath()); IOUtils.copy(in, out); } out.flush(); } catch (FileNotFoundException e) { throw new NotFoundException("Couldnt locate content"); } catch (ReadingException e) { throw new IOException(e); } catch (WritingException e) { throw new IOException(e); } finally { IOUtils.closeQuietly(in); } } /** * @{@inheritDoc} */ @Override public Long getMaxAgeSeconds(Auth auth) { return factory.maxAgeSeconds(this); } /** * @{@inheritDoc} */ @Override protected void doCopy(File dest) { try { FileUtils.copyFile(file, dest); } catch (IOException ex) { throw new RuntimeException("Failed doing copy to: " + dest.getAbsolutePath(), ex); } } @Deprecated @Override public void setProperties(Fields fields) { // MIL-50 // not implemented. Just to keep MS Office sweet } }