国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Spring源碼分析——資源訪問(wèn)利器Resource之實(shí)現(xiàn)類分析

2019-11-14 21:21:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
SPRing源碼分析——資源訪問(wèn)利器Resource之實(shí)現(xiàn)類分析

  今天來(lái)分析Spring的資源接口Resource的各個(gè)實(shí)現(xiàn)類。關(guān)于它的接口和抽象類,參見(jiàn)上一篇博文——Spring源碼分析——資源訪問(wèn)利器Resource之接口和抽象類分析

  一、文件系統(tǒng)資源 FileSystemResource

  文件系統(tǒng)資源 FileSystemResource,資源以文件系統(tǒng)路徑的方式表示。這個(gè)類繼承自AbstractResource,并實(shí)現(xiàn)了寫(xiě)的接口WritableResource。類全稱為public class FileSystemResource extends AbstractResource implements WritableResource 。這個(gè)資源類是所有Resource實(shí)現(xiàn)類中,唯一一個(gè)實(shí)現(xiàn)了WritableResource接口的類。就是說(shuō),其他的類都不可寫(xiě)入操作,都只能讀取。部分翻譯注釋后,源碼如下:(以后不重要的源碼我就折疊起來(lái)) 

/* * Copyright 2002-2012 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.springframework.core.io;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URI;import java.net.URL;import org.springframework.util.Assert;import org.springframework.util.StringUtils;/** * @author Juergen Hoeller * @since 28.12.2003 * @see java.io.File */public class FileSystemResource extends AbstractResource implements WritableResource {    private final File file;   //  不可變屬性    private final String path; //  不可變屬性    public FileSystemResource(File file) { //  簡(jiǎn)單的構(gòu)造方法,path為file路徑格式化后的樣子        Assert.notNull(file, "File must not be null");        this.file = file;        this.path = StringUtils.cleanPath(file.getPath());    }    public FileSystemResource(String path) {   //簡(jiǎn)單的構(gòu)造方法        Assert.notNull(path, "Path must not be null");        this.file = new File(path);        this.path = StringUtils.cleanPath(path);    }    public final String getPath() {    //新增的方法,返回資源路徑,方法不可重寫(xiě)        return this.path;    }    @Override    public boolean exists() {          return this.file.exists();    }    @Override    public boolean isReadable() {        return (this.file.canRead() && !this.file.isDirectory());    }    public InputStream getInputStream() throws IOException {   //InputStreamSource接口的實(shí)現(xiàn)方法        return new FileInputStream(this.file);    }    @Override    public URL getURL() throws IOException {   //可見(jiàn)這個(gè)url是通過(guò)uri得到的        return this.file.toURI().toURL();    }    @Override    public URI getURI() throws IOException {        return this.file.toURI();    }    @Override    public File getFile() {        return this.file;    }    @Override    public long contentLength() throws IOException {           return this.file.length();    }    @Override    public Resource createRelative(String relativePath) {        String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);        return new FileSystemResource(pathToUse);    }    @Override    public String getFilename() {        return this.file.getName();    }        public String getDescription() {   //  資源描述,直接用絕對(duì)路徑來(lái)構(gòu)造        return "file [" + this.file.getAbsolutePath() + "]";    }    public boolean isWritable() {  //  WritableResource接口的實(shí)現(xiàn)方法        return (this.file.canWrite() && !this.file.isDirectory());    }    public OutputStream getOutputStream() throws IOException {        return new FileOutputStream(this.file);    }    @Override    public boolean equals(Object obj) {    //通過(guò)path來(lái)比較        return (obj == this ||            (obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path)));    }    @Override    public int hashCode() {    //  文件資源的HashCode就是path的hashCode        return this.path.hashCode();    }}
View Code

結(jié)論:

  1、這個(gè)類由2個(gè)不可變的屬性 file 和 path ,本質(zhì)上就是一個(gè)java.io.File 的包裝

  2、值得一提的是,與父類AbstractResource不同的是,這個(gè)類的 equals() 和 hashcode() 都通過(guò)屬性 path 來(lái)操作。

  測(cè)試:

public class FileSytemResourceTest {    public static void main(String[] args) {        String path = "E:/java/abc.txt";        Resource resource = new FileSystemResource(path);        System.out.println("resource1 : "+resource.getFilename());                File f = new File("text.txt");        Resource resource2 = new FileSystemResource(f);        System.out.println("resource2 : "+resource2.getFilename());    }}

  結(jié)果:

resource1 : abc.txtresource2 : text.txt

二、字節(jié)數(shù)組資源——ByteArrayResource

  字節(jié)數(shù)組資源ByteArrayResource,資源即,字節(jié)數(shù)組。

  這個(gè)類很簡(jiǎn)單,也沒(méi)必要翻譯,僅僅是一個(gè)不可變的字節(jié)數(shù)組加一個(gè)不可變的描述字符串的包裝,源碼如下: 

/* * Copyright 2002-2012 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.springframework.core.io;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Arrays;/** * {@link Resource} implementation for a given byte array. * Creates a ByteArrayInputStreams for the given byte array. * * <p>Useful for loading content from any given byte array, * without having to resort to a single-use {@link InputStreamResource}. * Particularly useful for creating mail attachments from local content, * where JavaMail needs to be able to read the stream multiple times. * * @author Juergen Hoeller * @since 1.2.3 * @see java.io.ByteArrayInputStream * @see InputStreamResource * @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource) */public class ByteArrayResource extends AbstractResource {    private final byte[] byteArray;    private final String description;    /**     * Create a new ByteArrayResource.     * @param byteArray the byte array to wrap     */    public ByteArrayResource(byte[] byteArray) {        this(byteArray, "resource loaded from byte array");    }    /**     * Create a new ByteArrayResource.     * @param byteArray the byte array to wrap     * @param description where the byte array comes from     */    public ByteArrayResource(byte[] byteArray, String description) {        if (byteArray == null) {            throw new IllegalArgumentException("Byte array must not be null");        }        this.byteArray = byteArray;        this.description = (description != null ? description : "");    }    /**     * Return the underlying byte array.     */    public final byte[] getByteArray() {        return this.byteArray;    }    /**     * This implementation always returns {@code true}.     */    @Override    public boolean exists() {        return true;    }    /**     * This implementation returns the length of the underlying byte array.     */    @Override    public long contentLength() {        return this.byteArray.length;    }    /**     * This implementation returns a ByteArrayInputStream for the     * underlying byte array.     * @see java.io.ByteArrayInputStream     */    public InputStream getInputStream() throws IOException {        return new ByteArrayInputStream(this.byteArray);    }    /**     * This implementation returns the passed-in description, if any.     */    public String getDescription() {        return this.description;    }    /**     * This implementation compares the underlying byte array.     * @see java.util.Arrays#equals(byte[], byte[])     */    @Override    public boolean equals(Object obj) {        return (obj == this ||            (obj instanceof ByteArrayResource && Arrays.equals(((ByteArrayResource) obj).byteArray, this.byteArray)));    }    /**     * This implementation returns the hash code based on the     * underlying byte array.     */    @Override    public int hashCode() {        return (byte[].class.hashCode() * 29 * this.byteArray.length);    }}
View Code

  若需要操作描述一個(gè)字節(jié)數(shù)組,可以用這個(gè)資源類。ByteArrayResource可多次讀取數(shù)組資源。

三、描述性資源——DescriptiveResource  描述性資源DescriptiveResource,這個(gè)類更簡(jiǎn)單,僅僅一個(gè)不可變的描述字符串的包裝,源碼如下: 

/* * Copyright 2002-2012 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.springframework.core.io;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;/** * Simple {@link Resource} implementation that holds a resource description * but does not point to an actually readable resource. * * <p>To be used as placeholder if a {@code Resource} argument is * expected by an API but not necessarily used for actual reading. * * @author Juergen Hoeller * @since 1.2.6 */public class DescriptiveResource extends AbstractResource {    private final String description;    /**     * Create a new DescriptiveResource.     * @param description the resource description     */    public DescriptiveResource(String description) {        this.description = (description != null ? description : "");    }    @Override    public boolean exists() {        return false;    }    @Override    public boolean isReadable() {        return false;    }    public InputStream getInputStream() throws IOException {        throw new FileNotFoundException(                getDescription() + " cannot be opened because it does not point to a readable resource");    }    public String getDescription() {        return this.description;    }    /**     * This implementation compares the underlying description String.     */    @Override    public boolean equals(Object obj) {        return (obj == this ||            (obj instanceof DescriptiveResource && ((DescriptiveResource) obj).description.equals(this.description)));    }    /**     * This implementation returns the hash code of the underlying description String.     */    @Override    public int hashCode() {        return this.description.hashCode();    }}
View Code

  若一個(gè)資源,僅僅有一個(gè)描述,非常抽象的這種情況,可以用這個(gè)資源類,它并沒(méi)有指向一個(gè)實(shí)際的可讀的資源。一般用的非常稀少。個(gè)人覺(jué)得用處不大。 四、輸入流資源——InputStreamResource  輸入流資源InputStreamResource,是一個(gè)不可變InputStream的包裝和一個(gè)不可變的描述字符串。此外還有一個(gè)私有成員變量Boolean read用于限制本資源的InputStream不可被重復(fù)獲取。View Code 

/* * Copyright 2002-2012 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.springframework.core.io;import java.io.IOException;import java.io.InputStream;/** * {@link Resource} implementation for a given InputStream. Should only * be used if no specific Resource implementation is applicable. * In particular, prefer {@link ByteArrayResource} or any of the * file-based Resource implementations where possible. * * <p>In contrast to other Resource implementations, this is a descriptor * for an <i>already opened</i> resource - therefore returning "true" from * {@code isOpen()}. Do not use it if you need to keep the resource * descriptor somewhere, or if you need to read a stream multiple times. * * @author Juergen Hoeller * @since 28.12.2003 * @see ByteArrayResource * @see ClassPathResource * @see FileSystemResource * @see UrlResource */public class InputStreamResource extends AbstractResource {    private final InputStream inputStream;    private final String description;    private boolean read = false;    /**     * Create a new InputStreamResource.     * @param inputStream the InputStream to use     */    public InputStreamResource(InputStream inputStream) {        this(inputStream, "resource loaded through InputStream");    }    /**     * Create a new InputStreamResource.     * @param inputStream the InputStream to use     * @param description where the InputStream comes from     */    public InputStreamResource(InputStream inputStream, String description) {        if (inputStream == null) {            throw new IllegalArgumentException("InputStream must not be null");        }        this.inputStream = inputStream;        this.description = (description != null ? description : "");    }    /**     * This implementation always returns {@code true}.     */    @Override    public boolean exists() {        return true;    }    /**     * This implementation always returns {@code true}.     */    @Override    public boolean isOpen() {        return true;    }    /**     * This implementation throws IllegalStateException if attempting to     * read the underlying stream multiple times.     */    public InputStream getInputStream() throws IOException, IllegalStateException {        if (this.read) {            throw new IllegalStateException("InputStream has already been read - " +                    "do not use InputStreamResource if a stream needs to be read multiple times");        }        this.read = true;        return this.inputStream;    }    /**     * This implementation returns the passed-in description, if any.     */    public String getDescription() {        return this.description;    }    /**     * This implementation compares the underlying InputStream.     */    @Override    public boolean equals(Object obj) {        return (obj == this ||            (obj instanceof InputStreamResource && ((InputStreamResource) obj).inputStream.equals(this.inputStream)));    }    /**     * This implementation returns the hash code of the underlying InputStream.     */    @Override    public int hashCode() {        return this.inputStream.hashCode();    }}
View Code

  簡(jiǎn)單而言,這是一個(gè)InputStream的包裝類,這個(gè)包裝類指向的是一個(gè)已經(jīng)打開(kāi)的資源,所以它的 isOpen()總是返回true。而且它不能重復(fù)獲取資源,只能讀取一次。關(guān)閉資源也只能通過(guò)其中的InputStream來(lái)關(guān)閉。個(gè)人認(rèn)為,用處有限。五、VFS資源——VfsResource  vfs是Virtual File System虛擬文件系統(tǒng),也稱為虛擬文件系統(tǒng)開(kāi)關(guān)(Virtual Filesystem Switch).是linux檔案系統(tǒng)對(duì)外的接口。任何要使用檔案系統(tǒng)的程序都必須經(jīng)由這層接口來(lái)使用它。(摘自百度百科...)它能一致的訪問(wèn)物理文件系統(tǒng)、jar資源、zip資源、war資源等,VFS能把這些資源一致的映射到一個(gè)目錄上,訪問(wèn)它們就像訪問(wèn)物理文件資源一樣,而其實(shí)這些資源不存在于物理文件系統(tǒng)。    這個(gè)資源類包裝類一個(gè)Object對(duì)象,所有的操作都是通過(guò)這個(gè)包裝的對(duì)象的反射來(lái)實(shí)現(xiàn)的。這里就沒(méi)必要貼源碼了。  可以參考下面的用法:  

@Test  public void testVfsResourceForRealFileSystem() throws IOException {  //1.創(chuàng)建一個(gè)虛擬的文件目錄  VirtualFile home = VFS.getChild("/home");  //2.將虛擬目錄映射到物理的目錄  VFS.mount(home, new RealFileSystem(new File("d:")));  //3.通過(guò)虛擬目錄獲取文件資源  VirtualFile testFile = home.getChild("test.txt");  //4.通過(guò)一致的接口訪問(wèn)  Resource resource = new VfsResource(testFile);  if(resource.exists()) {          dumpStream(resource);  }  System.out.println("path:" + resource.getFile().getAbsolutePath());  Assert.assertEquals(false, resource.isOpen());         }  @Test  public void testVfsResourceForJar() throws IOException {  //1.首先獲取jar包路徑      File realFile = new File("lib/org.springframework.beans-3.0.5.RELEASE.jar");      //2.創(chuàng)建一個(gè)虛擬的文件目錄      VirtualFile home = VFS.getChild("/home2");      //3.將虛擬目錄映射到物理的目錄  VFS.mountZipExpanded(realFile, home,  TempFileProvider.create("tmp", Executors.newScheduledThreadPool(1)));  //4.通過(guò)虛擬目錄獲取文件資源      VirtualFile testFile = home.getChild("META-INF/spring.handlers");      Resource resource = new VfsResource(testFile);      if(resource.exists()) {              dumpStream(resource);      }      System.out.println("path:" + resource.getFile().getAbsolutePath());      Assert.assertEquals(false, resource.isOpen());  }  
View Code

六、Portlet上下文資源——PortletContextResource

  Portlet是基于java的web組件,由portlet容器管理,并由容器處理請(qǐng)求,生產(chǎn)動(dòng)態(tài)內(nèi)容。這個(gè)資源類封裝了一個(gè)不可變的javax.portlet.PortletContext對(duì)象和一個(gè)不可變的String對(duì)象代表路徑。類中所有操作都基于這兩個(gè)屬性。PortletContextResource對(duì)象實(shí)現(xiàn)了ContextResource接口,實(shí)現(xiàn)了方法String getPathWithinContext(),即返回自身的path屬性。

  源碼如下:

/* * Copyright 2002-2012 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.springframework.web.portlet.context;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import javax.portlet.PortletContext;import org.springframework.core.io.AbstractFileResolvingResource;import org.springframework.core.io.ContextResource;import org.springframework.core.io.Resource;import org.springframework.util.Assert;import org.springframework.util.ResourceUtils;import org.springframework.util.StringUtils;import org.springframework.web.portlet.util.PortletUtils;/** * {@link org.springframework.core.io.Resource} implementation for * {@link javax.portlet.PortletContext} resources, interpreting * relative paths within the portlet application root directory. * * <p>Always supports stream access and URL access, but only allows * {@code java.io.File} access when the portlet application archive * is expanded. * * @author Juergen Hoeller * @author John A. Lewis * @since 2.0 * @see javax.portlet.PortletContext#getResourceAsStream * @see javax.portlet.PortletContext#getRealPath */public class PortletContextResource extends AbstractFileResolvingResource implements ContextResource {    private final PortletContext portletContext;    private final String path;    /**     * Create a new PortletContextResource.     * <p>The Portlet spec requires that resource paths start with a slash,     * even if many containers accept paths without leading slash too.     * Consequently, the given path will be prepended with a slash if it     * doesn't already start with one.     * @param portletContext the PortletContext to load from     * @param path the path of the resource     */    public PortletContextResource(PortletContext portletContext, String path) {        // check PortletContext        Assert.notNull(portletContext, "Cannot resolve PortletContextResource without PortletContext");        this.portletContext = portletContext;        // check path        Assert.notNull(path, "Path is required");        String pathToUse = StringUtils.cleanPath(path);        if (!pathToUse.startsWith("/")) {            pathToUse = "/" + pathToUse;        }        this.path = pathToUse;    }    /**     * Return the PortletContext for this resource.     */    public final PortletContext getPortletContext() {        return this.portletContext;    }    /**     * Return the path for this resource.     */    public final String getPath() {        return this.path;    }    /**     * This implementation checks {@code PortletContext.getResource}.     * @see javax.portlet.PortletContext#getResource(String)     */    @Override    public boolean exists() {        try {            URL url = this.portletContext.getResource(this.path);            return (url != null);        }        catch (MalformedURLException ex) {            return false;        }    }    /**     * This implementation delegates to {@code PortletContext.getResourceAsStream},     * which returns {@code null} in case of a non-readable resource (e.g. a directory).     * @see javax.portlet.PortletContext#getResourceAsStream(String)     */    @Override    public boolean isReadable() {        InputStream is = this.portletContext.getResourceAsStream(this.path);        if (is != null) {            try {                is.close();            }            catch (IOException ex) {                // ignore            }            return true;        }        else {            return false;        }    }    /**     * This implementation delegates to {@code PortletContext.getResourceAsStream},     * but throws a FileNotFoundException if not found.     * @see javax.portlet.PortletContext#getResourceAsStream(String)     */    public InputStream getInputStream() throws IOException {        InputStream is = this.portletContext.getResourceAsStream(this.path);        if (is == null) {            throw new FileNotFoundException("Could not open " + getDescription());        }        return is;    }    /**     * This implementation delegates to {@code PortletContext.getResource},     * but throws a FileNotFoundException if no resource found.     * @see javax.portlet.PortletContext#getResource(String)     */    @Override    public URL getURL() throws IOException {        URL url = this.portletContext.getResource(this.path);        if (url == null) {            throw new FileNotFoundException(                    getDescription() + " cannot be resolved to URL because it does not exist");        }        return url;    }    /**     * This implementation resolves "file:" URLs or alternatively delegates to     * {@code PortletContext.getRealPath}, throwing a FileNotFoundException     * if not found or not resolvable.     * @see javax.portlet.PortletContext#getResource(String)     * @see javax.portlet.PortletContext#getRealPath(String)     */    @Override    public File getFile() throws IOException {        URL url = getURL();        if (ResourceUtils.isFileURL(url)) {            // Proceed with file system resolution...            return super.getFile();        }        else {            String realPath = PortletUtils.getRealPath(this.portletContext, this.path);            return new File(realPath);        }    }    @Override    public Resource createRelative(String relativePath) {        String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);        return new PortletContextResource(this.portletContext, pathToUse);    }    @Override    public String getFilename() {        return StringUtils.getFilename(this.path);    }    public String getDescription() {        return "PortletContext resource [" + this.path + "]";    }    public String getPathWithinContext() {        return this.path;    }    @Override    public boolean equals(Object obj) {        if (obj == this) {            return true;        }        if (obj instanceof PortletContextResource) {            PortletContextResource otherRes = (PortletContextResource) obj;            return (this.portletContext.equals(otherRes.portletContext) && this.path.equals(otherRes.path));        }        return false;    }    @Override    public int hashCode() {        return this.path.hashCode();    }}
View Code

這個(gè)類非常簡(jiǎn)單,并沒(méi)有什么需要注意的。

七、Servlet上下文資源——ServletContextResource

  Servlet這個(gè)大家都知道。這個(gè)資源類是為了訪問(wèn)Web容器上下文的資源而封裝的類,可以以相對(duì)于Web應(yīng)用根目錄的路徑加載資源。這個(gè)資源類封裝了一個(gè)不可變的javax.servlet.ServletContext對(duì)象和一個(gè)不可變的String對(duì)象代表路徑。類中所有操作都基于這兩個(gè)屬性。PortletContextResource對(duì)象實(shí)現(xiàn)了ContextResource接口,實(shí)現(xiàn)了方法String getPathWithinContext(),即返回自身的path屬性。

  這個(gè)類的實(shí)現(xiàn)基本就是基于 this.servletContext.getResource(this.path) 或 this.servletContext.getResourceAsStream(this.path) 這兩個(gè)方法。

典型的,例如這個(gè)方法:

public InputStream getInputStream() throws IOException {        InputStream is = this.servletContext.getResourceAsStream(this.path);        if (is == null) {            throw new FileNotFoundException("Could not open " + getDescription());        }        return is;    }

  又如這個(gè)方法:

public URL getURL() throws IOException {        URL url = this.servletContext.getResource(this.path);        if (url == null) {            throw new FileNotFoundException(                    getDescription() + " cannot be resolved to URL because it does not exist");        }        return url;    }

貼一下源碼:

/* * Copyright 2002-2012 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.springframework.web.context.support;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import javax.servlet.ServletContext;import org.springframework.core.io.AbstractFileResolvingResource;import org.springframework.core.io.ContextResource;import org.springframework.core.io.Resource;import org.springframework.util.Assert;import org.springframework.util.ResourceUtils;import org.springframework.util.StringUtils;import org.springframework.web.util.WebUtils;/** * {@link org.springframework.core.io.Resource} implementation for * {@link javax.servlet.ServletContext} resources, interpreting * relative paths within the web application root directory. * * <p>Always supports stream access and URL access, but only allows * {@code java.io.File} access when the web application archive * is expanded. * * @author Juergen Hoeller * @since 28.12.2003 * @see javax.servlet.ServletContext#getResourceAsStream * @see javax.servlet.ServletContext#getResource * @see javax.servlet.ServletContext#getRealPath */public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource {    private final ServletContext servletContext;    private final String path;    /**     * Create a new ServletContextResource.     * <p>The Servlet spec requires that resource paths start with a slash,     * even if many containers accept paths without leading slash too.     * Consequently, the given path will be prepended with a slash if it     * doesn't already start with one.     * @param servletContext the ServletContext to load from     * @param path the path of the resource     */    public ServletContextResource(ServletContext servletContext, String path) {        // check ServletContext        Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext");        this.servletContext = servletContext;        // check path        Assert.notNull(path, "Path is required");        String pathToUse = StringUtils.cleanPath(path);        if (!pathToUse.startsWith("/")) {            pathToUse = "/" + pathToUse;        }        this.path = pathToUse;    }    /**     * Return the ServletContext for this resource.     */    public final ServletContext getServletContext() {        return this.servletContext;    }    /**     * Return the path for this resource.     */    public final String getPath() {        return this.path;    }    /**     * This implementation checks {@code ServletContext.getResource}.     * @see javax.servlet.ServletContext#getResource(String)     */    @Override    public boolean exists() {        try {            URL url = this.servletContext.getResource(this.path);            return (url != null);        }        catch (MalformedURLException ex) {            return false;        }    }    /**     * This implementation delegates to {@code ServletContext.getResourceAsStream},     * which returns {@code null} in case of a non-readable resource (e.g. a directory).     * @see javax.servlet.ServletContext#getResourceAsStream(String)     */    @Override    public boolean isReadable() {        InputStream is = this.servletContext.getResourceAsStream(this.path);        if (is != null) {            try {                is.close();            }            catch (IOException ex) {                // ignore            }            return true;        }        else {            return false;        }    }    /**     * This implementation delegates to {@code ServletContext.getResourceAsStream},     * but throws a FileNotFoundException if no resource found.     * @see javax.servlet.ServletContext#getResourceAsStream(String)     */    public InputStream getInputStream() throws IOException {        InputStream is = this.servletContext.getResourceAsStream(this.path);        if (is == null) {            throw new FileNotFoundException("Could not open " + getDescription());        }        return is;    }    /**     * This implementation delegates to {@code ServletContext.getResource},     * but throws a FileNotFoundException if no resource found.     * @see javax.servlet.ServletContext#getResource(String)     */    @Override    public URL getURL() throws IOException {        URL url = this.servletContext.getResource(this.path);        if (url == null) {            throw new FileNotFoundException(                    getDescription() + " cannot be resolved to URL because it does not exist");        }        return url;    }    /**     * This implementation resolves "file:" URLs or alternatively delegate
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高安市| 宿迁市| 洛隆县| 若羌县| 桦川县| 古浪县| 鹤壁市| 双江| 庆阳市| 壤塘县| 新平| 双桥区| 洞口县| 略阳县| 阜新市| 文昌市| 汶川县| 南汇区| 都昌县| 内黄县| 沛县| 集贤县| 如东县| 北安市| 秦安县| 汕头市| 射阳县| 常州市| 湘乡市| 尉氏县| 萝北县| 福贡县| 湖北省| 上林县| 巢湖市| 龙泉市| 孝义市| 克东县| 睢宁县| 鞍山市| 宝坻区|