001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. 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 distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest; 014 015import static org.apache.juneau.internal.CollectionUtils.*; 016import static org.apache.juneau.internal.IOUtils.*; 017 018import java.io.*; 019import java.util.*; 020 021import org.apache.juneau.*; 022import org.apache.juneau.http.*; 023import org.apache.juneau.svl.*; 024 025/** 026 * @deprecated Use {@link org.apache.juneau.http.ReaderResource} 027 */ 028@Deprecated 029public class ReaderResource implements Writable { 030 031 private final MediaType mediaType; 032 private final String[] contents; 033 private final VarResolverSession varSession; 034 private final Map<String,Object> headers; 035 036 /** 037 * Creates a new instance of a {@link ReaderResourceBuilder} 038 * 039 * @return A new instance of a {@link ReaderResourceBuilder} 040 */ 041 public static ReaderResourceBuilder create() { 042 return new ReaderResourceBuilder(); 043 } 044 045 /** 046 * Constructor. 047 * 048 * @param mediaType The resource media type. 049 * @param headers The HTTP response headers for this streamed resource. 050 * @param varSession Optional variable resolver for resolving variables in the string. 051 * @param contents 052 * The resource contents. 053 * <br>If multiple contents are specified, the results will be concatenated. 054 * <br>Contents can be any of the following: 055 * <ul> 056 * <li><code>InputStream</code> 057 * <li><code>Reader</code> - Converted to UTF-8 bytes. 058 * <li><code>File</code> 059 * <li><code>CharSequence</code> - Converted to UTF-8 bytes. 060 * </ul> 061 * @throws IOException 062 */ 063 public ReaderResource(MediaType mediaType, Map<String,Object> headers, VarResolverSession varSession, Object...contents) throws IOException { 064 this.mediaType = mediaType; 065 this.varSession = varSession; 066 067 this.headers = immutableMap(headers); 068 069 this.contents = new String[contents.length]; 070 for (int i = 0; i < contents.length; i++) { 071 Object c = contents[i]; 072 if (c == null) 073 this.contents[i] = ""; 074 else if (c instanceof InputStream) 075 this.contents[i] = read((InputStream)c); 076 else if (c instanceof File) 077 this.contents[i] = read((File)c); 078 else if (c instanceof Reader) 079 this.contents[i] = read((Reader)c); 080 else if (c instanceof CharSequence) 081 this.contents[i] = ((CharSequence)c).toString(); 082 else 083 throw new IOException("Invalid class type passed to ReaderResource: " + c.getClass().getName()); 084 } 085 } 086 087 /** 088 * Get the HTTP response headers. 089 * 090 * @return 091 * The HTTP response headers. 092 * <br>An unmodifiable map. 093 * <br>Never <jk>null</jk>. 094 */ 095 public Map<String,Object> getHeaders() { 096 return headers; 097 } 098 099 @Override /* Writeable */ 100 public Writer writeTo(Writer w) throws IOException { 101 for (String s : contents) { 102 if (varSession != null) 103 varSession.resolveTo(s, w); 104 else 105 w.write(s); 106 } 107 return w; 108 } 109 110 @Override /* Writeable */ 111 public MediaType getMediaType() { 112 return mediaType; 113 } 114 115 @Override /* Object */ 116 public String toString() { 117 if (contents.length == 1 && varSession == null) 118 return contents[0]; 119 StringWriter sw = new StringWriter(); 120 for (String s : contents) { 121 if (varSession != null) 122 return varSession.resolve(s); 123 sw.write(s); 124 } 125 return sw.toString(); 126 } 127 128 /** 129 * Same as {@link #toString()} but strips comments from the text before returning it. 130 * 131 * <p> 132 * Supports stripping comments from the following media types: HTML, XHTML, XML, JSON, Javascript, CSS. 133 * 134 * @return The resource contents stripped of any comments. 135 */ 136 public String toCommentStrippedString() { 137 String s = toString(); 138 String subType = mediaType.getSubType(); 139 if ("html".equals(subType) || "xhtml".equals(subType) || "xml".equals(subType)) 140 s = s.replaceAll("(?s)<!--(.*?)-->\\s*", ""); 141 else if ("json".equals(subType) || "javascript".equals(subType) || "css".equals(subType)) 142 s = s.replaceAll("(?s)\\/\\*(.*?)\\*\\/\\s*", ""); 143 return s; 144 } 145}