001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.request.resource;
018
019import java.nio.charset.Charset;
020
021import org.apache.wicket.request.Response;
022import org.apache.wicket.util.string.Strings;
023
024/**
025 * An {@link org.apache.wicket.request.resource.IResource} for CharSequences.
026 * The char sequence can be static - passed to the constructor,
027 * or dynamic - by overriding {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
028 */
029public class CharSequenceResource extends BaseDataResource<CharSequence>
030{
031        private static final long serialVersionUID = 1L;
032
033        /** Charset name for resource */
034        private String charsetName;
035
036        /**
037         * Creates a {@link org.apache.wicket.request.resource.CharSequenceResource} which will provide its data dynamically with
038         * {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
039         *
040         * @param contentType
041         *            The Content type of the array.
042         */
043        public CharSequenceResource(final String contentType)
044        {
045                super(contentType);
046        }
047
048        /**
049         * Creates a Resource from the given char sequence with its content type
050         *
051         * @param contentType
052         *            The Content type of the array.
053         * @param data
054         *            The data
055         */
056        public CharSequenceResource(final String contentType, final CharSequence data)
057        {
058                super(contentType, data);
059        }
060
061        /**
062         * Creates a Resource from the given char sequence with its content type
063         *
064         * @param contentType
065         *            The Content type of the array.
066         * @param data
067         *            The data
068         * @param filename
069         *            The filename that will be set as the Content-Disposition header.
070         */
071        public CharSequenceResource(final String contentType, final CharSequence data, final String filename)
072        {
073                super(contentType, data, filename);
074        }
075
076        @Override
077        protected void writeData(Response response, CharSequence data)
078        {
079                response.write(data);
080        }
081
082        @Override
083        protected Long getLength(CharSequence data)
084        {
085                return (long) Strings.lengthInBytes(data.toString(), getCharset());
086        }
087
088        /**
089         * @return Charset for resource
090         */
091        protected Charset getCharset()
092        {
093                return (charsetName != null) ? Charset.forName(charsetName) : null;
094        }
095
096        /**
097         * Sets the character set used for reading this resource.
098         *
099         * @param charset
100         *            Charset for component
101         */
102        public void setCharset(final Charset charset)
103        {
104                // java.nio.Charset itself is not serializable so we can only store the name
105                charsetName = (charset != null) ? charset.name() : null;
106        }
107
108}