View Javadoc

    1   /*******************************************************************************
    2    * Copyright (c) 2011 Michael Mimo Moratti.
    3    *
    4    * Michael Mimo Moratti licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at:
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    * Unless required by applicable law or agreed to in writing, software
    9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    11   * License for the specific language governing permissions and limitations
    12   * under the License.
    13   *******************************************************************************/
    14  package ch.mimo.netty.handler.codec.icap;
    15
    16  import java.util.Map;
    17  import java.util.Map.Entry;
    18  import java.util.Set;
    19
    20  import org.jboss.netty.handler.codec.http.HttpRequest;
    21  import org.jboss.netty.handler.codec.http.HttpResponse;
    22  import org.jboss.netty.util.internal.StringUtil;
    23
    24  /**
    25   * This is the main Icap message implementation where
    26   * all common @see {@link DefaultIcapRequest} and @see {@link DefaultIcapResponse} member are present.
    27   *
    28   * @author Michael Mimo Moratti (mimo@mimo.ch)
    29   *
    30   */
    31  public abstract class AbstractIcapMessage implements IcapMessage {
    32
    33  	private IcapHeaders icapHeader;
    34  	private IcapVersion version;
    35  	private Encapsulated encapsulated;
    36
    37  	private HttpRequest httpRequest;
    38  	private HttpResponse httpResponse;
    39
    40  	private IcapMessageElementEnum body;
    41
    42  	public AbstractIcapMessage(IcapVersion version) {
    43  		this.version = version;
    44  		icapHeader = new IcapHeaders();
    45  	}
    46
    47  	@Override
    48  	public String getHeader(String name) {
    49  		return icapHeader.getHeader(name);
    50  	}
    51
    52  	@Override
    53  	public Set<String> getHeaders(String name) {
    54  		return icapHeader.getHeaders(name);
    55  	}
    56
    57  	@Override
    58  	public Set<Entry<String, String>> getHeaders() {
    59  		return icapHeader.getHeaders();
    60  	}
    61
    62  	@Override
    63  	public boolean containsHeader(String name) {
    64  		return icapHeader.containsHeader(name);
    65  	}
    66
    67  	@Override
    68  	public Set<String> getHeaderNames() {
    69  		return icapHeader.getHeaderNames();
    70  	}
    71
    72  	@Override
    73  	public IcapMessage addHeader(String name, Object value) {
    74  		icapHeader.addHeader(name,value);
    75  		return this;
    76  	}
    77
    78  	@Override
    79  	public IcapMessage setHeader(String name, Object value) {
    80  		icapHeader.setHeader(name,value);
    81  		return this;
    82  	}
    83
    84  	@Override
    85  	public IcapMessage setHeader(String name, Iterable<?> values) {
    86  		icapHeader.setHeader(name,values);
    87  		return this;
    88  	}
    89
    90  	@Override
    91  	public IcapMessage removeHeader(String name) {
    92  		icapHeader.removeHeader(name);
    93  		return this;
    94  	}
    95
    96  	@Override
    97  	public int getPreviewAmount() {
    98  		return icapHeader.getPreviewHeaderValue();
    99  	}
    100
    101 	@Override
    102 	public IcapMessage clearHeaders() {
    103 		icapHeader.clearHeaders();
    104 		return this;
    105 	}
    106
    107 	@Override
    108 	public IcapVersion getProtocolVersion() {
    109 		return version;
    110 	}
    111
    112 	@Override
    113 	public IcapMessage setProtocolVersion(IcapVersion version) {
    114 		this.version = version;
    115 		return this;
    116 	}
    117
    118 	@Override
    119 	public boolean containsHttpRequest() {
    120 		return httpRequest != null;
    121 	}
    122
    123 	@Override
    124 	public HttpRequest getHttpRequest() {
    125 		return httpRequest;
    126 	}
    127
    128 	@Override
    129 	public IcapMessage setHttpRequest(HttpRequest httpRequest) {
    130 		this.httpRequest = httpRequest;
    131 		return this;
    132 	}
    133
    134 	@Override
    135 	public boolean containsHttpResponse() {
    136 		return httpResponse != null;
    137 	}
    138
    139 	@Override
    140 	public HttpResponse getHttpResponse() {
    141 		return httpResponse;
    142 	}
    143
    144 	public IcapMessage setHttpResponse(HttpResponse response) {
    145 		this.httpResponse = response;
    146 		return this;
    147 	}
    148
    149 	@Override
    150 	public IcapMessage setEncapsulatedHeader(Encapsulated encapsulated) {
    151 		this.encapsulated = encapsulated;
    152 		return this;
    153 	}
    154
    155 	@Override
    156 	public Encapsulated getEncapsulatedHeader() {
    157 		return encapsulated;
    158 	}
    159
    160 	@Override
    161 	public boolean isPreviewMessage() {
    162 		return icapHeader.getPreviewHeaderValue() > 0;
    163 	}
    164
    165 	public IcapMessage setBody(IcapMessageElementEnum body) {
    166 		this.body = body;
    167 		return this;
    168 	}
    169
    170 	public IcapMessageElementEnum getBodyType() {
    171 		if(encapsulated != null) {
    172 			return encapsulated.containsBodyEntry();
    173 		} else {
    174 			return body;
    175 		}
    176 	}
    177
    178     @Override
    179     public String toString() {
    180         StringBuilder buf = new StringBuilder();
    181         buf.append(getClass().getSimpleName());
    182         buf.append("(version: ");
    183         buf.append(getProtocolVersion().getText());
    184         buf.append(')');
    185         buf.append(StringUtil.NEWLINE);
    186         appendHeaders(buf);
    187
    188         if(httpRequest != null) {
    189         	buf.append("--- encapsulated HTTP Request ---").append(StringUtil.NEWLINE);
    190         	buf.append(httpRequest.toString());
    191         	if(httpRequest.getContent() != null && httpRequest.getContent().readableBytes() > 0) {
    192         		buf.append(StringUtil.NEWLINE).append("--> HTTP Request contains [" + httpRequest.getContent().readableBytes() + "] bytes of data").append(StringUtil.NEWLINE);
    193         	}
    194         }
    195
    196         if(httpResponse != null) {
    197         	buf.append("--- encapsulated HTTP Response ---").append(StringUtil.NEWLINE);
    198         	buf.append(httpResponse.toString());
    199         	if(httpResponse.getContent() != null && httpResponse.getContent().readableBytes() > 0) {
    200         		buf.append(StringUtil.NEWLINE).append("--> HTTP Response contains [" + httpResponse.getContent().readableBytes() + "] bytes of data").append(StringUtil.NEWLINE);;
    201         	}
    202         }
    203
    204         if(isPreviewMessage()) {
    205         	buf.append("--- Preview ---").append(StringUtil.NEWLINE);
    206         	buf.append("Preview size: " + icapHeader.getPreviewHeaderValue());
    207         }
    208
    209         return buf.toString();
    210     }
    211
    212     private void appendHeaders(StringBuilder buf) {
    213         for (Map.Entry<String, String> e: getHeaders()) {
    214             buf.append(e.getKey());
    215             buf.append(": ");
    216             buf.append(e.getValue());
    217             buf.append(StringUtil.NEWLINE);
    218         }
    219     }
    220 }