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.juneau.http.classic.resource; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import org.apache.juneau.http.UnmodifiableBean; 022import org.apache.juneau.http.classic.entity.*; 023import org.apache.juneau.http.classic.header.*; 024 025/** 026 * A self contained, repeatable resource that obtains its content from a {@link String}. 027 * 028 * <h5 class='section'>See Also:</h5><ul> 029 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a> 030 * </ul> 031 */ 032public class StringResource extends BasicResource<StringResource> { 033 034 /** 035 * Constructor. 036 */ 037 public StringResource() { 038 super(new StringEntity()); 039 } 040 041 /** 042 * Constructor. 043 * 044 * @param contentType The entity content type. Can be <jk>null</jk> to omit an explicit <c>Content-Type</c> header. 045 * @param contents The entity contents. Can be <jk>null</jk> (treated as an empty string). 046 */ 047 public StringResource(ContentType contentType, String contents) { 048 super(new StringEntity(contentType, contents)); 049 } 050 051 /** 052 * Copy constructor. 053 * 054 * @param copyFrom The bean being copied. Must not be <jk>null</jk>. 055 */ 056 protected StringResource(StringResource copyFrom) { 057 super(copyFrom); 058 } 059 060 @Override /* Overridden from BasicResource */ 061 public StringResource copy() { 062 return new StringResource(this); 063 } 064 065 @Override /* Overridden from BasicResource */ 066 public StringResource unmodifiable() { 067 return this instanceof UnmodifiableBean ? this : new Unmodifiable(this); 068 } 069 070 /** 071 * An unmodifiable snapshot of a {@link StringResource}. 072 */ 073 public static class Unmodifiable extends StringResource implements UnmodifiableBean { 074 075 /** 076 * Constructor. 077 * 078 * @param copyFrom The bean to snapshot-copy. Must not be <jk>null</jk>. 079 */ 080 @SuppressWarnings({ 081 "java:S1699" // Paradigm intentionally calls the overridable freeze() from the ctor to deep-freeze sub-beans. 082 }) 083 protected Unmodifiable(StringResource copyFrom) { 084 super(copyFrom); 085 freeze(); 086 } 087 088 @Override /* Overridden from BasicResource */ 089 protected StringResource modify(Runnable mutation) { 090 throw uoex("Bean is unmodifiable."); 091 } 092 } 093}