001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.propertyeditors;
018
019import java.beans.PropertyEditorSupport;
020import java.io.IOException;
021
022import org.springframework.core.io.Resource;
023import org.springframework.core.io.ResourceEditor;
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026
027/**
028 * One-way PropertyEditor which can convert from a text String to a
029 * {@code java.io.InputStream}, interpreting the given String as a
030 * Spring resource location (e.g. a URL String).
031 *
032 * <p>Supports Spring-style URL notation: any fully qualified standard URL
033 * ("file:", "http:", etc.) and Spring's special "classpath:" pseudo-URL.
034 *
035 * <p>Note that such streams usually do not get closed by Spring itself!
036 *
037 * @author Juergen Hoeller
038 * @since 1.0.1
039 * @see java.io.InputStream
040 * @see org.springframework.core.io.ResourceEditor
041 * @see org.springframework.core.io.ResourceLoader
042 * @see URLEditor
043 * @see FileEditor
044 */
045public class InputStreamEditor extends PropertyEditorSupport {
046
047        private final ResourceEditor resourceEditor;
048
049
050        /**
051         * Create a new InputStreamEditor, using the default ResourceEditor underneath.
052         */
053        public InputStreamEditor() {
054                this.resourceEditor = new ResourceEditor();
055        }
056
057        /**
058         * Create a new InputStreamEditor, using the given ResourceEditor underneath.
059         * @param resourceEditor the ResourceEditor to use
060         */
061        public InputStreamEditor(ResourceEditor resourceEditor) {
062                Assert.notNull(resourceEditor, "ResourceEditor must not be null");
063                this.resourceEditor = resourceEditor;
064        }
065
066
067        @Override
068        public void setAsText(String text) throws IllegalArgumentException {
069                this.resourceEditor.setAsText(text);
070                Resource resource = (Resource) this.resourceEditor.getValue();
071                try {
072                        setValue(resource != null ? resource.getInputStream() : null);
073                }
074                catch (IOException ex) {
075                        throw new IllegalArgumentException("Failed to retrieve InputStream for " + resource, ex);
076                }
077        }
078
079        /**
080         * This implementation returns {@code null} to indicate that
081         * there is no appropriate text representation.
082         */
083        @Override
084        @Nullable
085        public String getAsText() {
086                return null;
087        }
088
089}