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.helper; 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.http.annotation.*; 024import org.apache.juneau.svl.*; 025 026/** 027 * @deprecated Use {@link org.apache.juneau.http.ReaderResource} 028 */ 029@Response 030@Deprecated 031public class ReaderResource implements Writable { 032 033 private final MediaType mediaType; 034 private final String[] contents; 035 private final VarResolverSession varSession; 036 private final Map<String,Object> headers; 037 038 /** 039 * Creates a new instance of a {@link ReaderResourceBuilder} 040 * 041 * @return A new instance of a {@link ReaderResourceBuilder} 042 */ 043 public static ReaderResourceBuilder create() { 044 return new ReaderResourceBuilder(); 045 } 046 047 /** 048 * Constructor. 049 * 050 * @param mediaType The resource media type. 051 * @param headers The HTTP response headers for this streamed resource. 052 * @param varSession Optional variable resolver for resolving variables in the string. 053 * @param contents 054 * The resource contents. 055 * <br>If multiple contents are specified, the results will be concatenated. 056 * <br>Contents can be any of the following: 057 * <ul> 058 * <li><code>InputStream</code> 059 * <li><code>Reader</code> - Converted to UTF-8 bytes. 060 * <li><code>File</code> 061 * <li><code>CharSequence</code> - Converted to UTF-8 bytes. 062 * </ul> 063 * @throws IOException 064 */ 065 public ReaderResource(MediaType mediaType, Map<String,Object> headers, VarResolverSession varSession, Object...contents) throws IOException { 066 this.mediaType = mediaType; 067 this.varSession = varSession; 068 069 this.headers = immutableMap(headers); 070 071 this.contents = new String[contents.length]; 072 for (int i = 0; i < contents.length; i++) { 073 Object c = contents[i]; 074 if (c == null) 075 this.contents[i] = ""; 076 else if (c instanceof InputStream) 077 this.contents[i] = read((InputStream)c); 078 else if (c instanceof File) 079 this.contents[i] = read((File)c); 080 else if (c instanceof Reader) 081 this.contents[i] = read((Reader)c); 082 else if (c instanceof CharSequence) 083 this.contents[i] = ((CharSequence)c).toString(); 084 else 085 throw new IOException("Invalid class type passed to ReaderResource: " + c.getClass().getName()); 086 } 087 } 088 089 /** 090 * Get the HTTP response headers. 091 * 092 * @return 093 * The HTTP response headers. 094 * <br>An unmodifiable map. 095 * <br>Never <jk>null</jk>. 096 */ 097 @ResponseHeader("*") 098 public Map<String,Object> getHeaders() { 099 return headers; 100 } 101 102 @ResponseBody 103 @Override /* Writeable */ 104 public Writer writeTo(Writer w) throws IOException { 105 for (String s : contents) { 106 if (varSession != null) 107 varSession.resolveTo(s, w); 108 else 109 w.write(s); 110 } 111 return w; 112 } 113 114 @ResponseHeader("Content-Type") 115 @Override /* Writeable */ 116 public MediaType getMediaType() { 117 return mediaType; 118 } 119 120 @Override /* Object */ 121 public String toString() { 122 if (contents.length == 1 && varSession == null) 123 return contents[0]; 124 StringWriter sw = new StringWriter(); 125 for (String s : contents) { 126 if (varSession != null) 127 return varSession.resolve(s); 128 sw.write(s); 129 } 130 return sw.toString(); 131 } 132 133 /** 134 * Same as {@link #toString()} but strips comments from the text before returning it. 135 * 136 * <p> 137 * Supports stripping comments from the following media types: HTML, XHTML, XML, JSON, Javascript, CSS. 138 * 139 * @return The resource contents stripped of any comments. 140 */ 141 public String toCommentStrippedString() { 142 String s = toString(); 143 String subType = mediaType.getSubType(); 144 if ("html".equals(subType) || "xhtml".equals(subType) || "xml".equals(subType)) 145 s = s.replaceAll("(?s)<!--(.*?)-->\\s*", ""); 146 else if ("json".equals(subType) || "javascript".equals(subType) || "css".equals(subType)) 147 s = s.replaceAll("(?s)\\/\\*(.*?)\\*\\/\\s*", ""); 148 return s; 149 } 150}