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.handler; 018 019import java.io.IOException; 020import java.nio.charset.Charset; 021 022import org.apache.wicket.request.IRequestCycle; 023import org.apache.wicket.request.IRequestHandler; 024import org.apache.wicket.request.http.WebResponse; 025import org.apache.wicket.util.lang.Args; 026import org.apache.wicket.util.string.Strings; 027 028 029/** 030 * Request target that responds by sending its string property. 031 * 032 * @author igor.vaynberg 033 * @author Eelco Hillenius 034 */ 035public class TextRequestHandler implements IRequestHandler 036{ 037 /** the string for the response. */ 038 private final String string; 039 040 /** content type for the string */ 041 private final String contentType; 042 043 /** charset of the string */ 044 private final String encoding; 045 046 047 /** 048 * Creates a string request target with content type <code>text/plain</code> and default charset 049 * (usually UTF-8) 050 * 051 * @param string 052 * the string for the response 053 */ 054 public TextRequestHandler(final String string) 055 { 056 this("text/plain", null, string); 057 } 058 059 /** 060 * Constructor 061 * 062 * @param contentType 063 * content type of the data the string represents, e.g. 064 * <code>text/html; charset=utf-8</code> 065 * @param encoding 066 * charset to use 067 * @param string 068 * string for the response 069 */ 070 public TextRequestHandler(final String contentType, final String encoding, final String string) 071 { 072 this.contentType = Args.notEmpty(contentType, "contentType"); 073 this.string = Args.notNull(string, "string"); 074 this.encoding = encoding; 075 } 076 077 078 /** 079 * Responds by sending the string property. 080 * 081 * @see org.apache.wicket.request.IRequestHandler#respond(org.apache.wicket.request.IRequestCycle) 082 */ 083 @Override 084 public void respond(final IRequestCycle requestCycle) 085 { 086 String encoding = getEncoding(requestCycle); 087 088 // Get servlet response to use when responding with resource 089 final WebResponse response = (WebResponse)requestCycle.getResponse(); 090 response.setContentType(contentType + ";charset=" + encoding); 091 092 // send string to client 093 try 094 { 095 byte[] bytes = string.getBytes(encoding); 096 response.setContentLength(bytes.length); 097 response.write(bytes); 098 } 099 catch (IOException e) 100 { 101 throw new RuntimeException("Unable to render string: " + e.getMessage(), e); 102 } 103 } 104 105 /** 106 * @param requestCycle 107 * @return the configured encoding or the request's one as default 108 */ 109 private String getEncoding(final IRequestCycle requestCycle) 110 { 111 String encoding = this.encoding; 112 if (Strings.isEmpty(encoding)) 113 { 114 Charset charset = requestCycle.getRequest().getCharset(); 115 if (charset != null) 116 { 117 encoding = charset.name(); 118 } 119 } 120 return encoding; 121 } 122 123 /** 124 * Gets the string property. 125 * 126 * @return the string property 127 */ 128 public String getString() 129 { 130 return string; 131 } 132 133}