001// Copyright 2007, 2008 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.upload.internal.services; 016 017import org.apache.commons.fileupload.FileItem; 018import org.apache.commons.io.FilenameUtils; 019import org.apache.tapestry5.upload.services.UploadedFile; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.InputStream; 024 025/** 026 * Implentation of {@link org.apache.tapestry5.upload.services.UploadedFile} for FileItems. 027 */ 028public class UploadedFileItem implements UploadedFile 029{ 030 private final FileItem item; 031 032 public UploadedFileItem(FileItem item) 033 { 034 this.item = item; 035 } 036 037 public String getContentType() 038 { 039 return item.getContentType(); 040 } 041 042 public String getFileName() 043 { 044 return FilenameUtils.getName(getFilePath()); 045 } 046 047 public String getFilePath() 048 { 049 return item.getName(); 050 } 051 052 public long getSize() 053 { 054 return item.getSize(); 055 } 056 057 public InputStream getStream() 058 { 059 try 060 { 061 return item.getInputStream(); 062 } 063 catch (IOException e) 064 { 065 throw new RuntimeException(UploadMessages.unableToOpenContentFile(this), e); 066 } 067 } 068 069 public boolean isInMemory() 070 { 071 return item.isInMemory(); 072 } 073 074 public void write(File file) 075 { 076 try 077 { 078 item.write(file); 079 } 080 catch (Exception e) 081 { 082 throw new RuntimeException(UploadMessages.writeFailure(file), e); 083 } 084 } 085 086 public void cleanup() 087 { 088 item.delete(); 089 } 090}