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 org.jboss.netty.buffer.ChannelBuffer;
17  
18  /**
19   * ICAP response status enum. contains all valid response codes. like 200, 204 and others.
20   * 
21   * @author Michael Mimo Moratti (mimo@mimo.ch)
22   *
23   */
24  public enum IcapResponseStatus {
25  	CONTINUE(100,"Continue"),
26  	SWITCHING_PROTOCOLS(101,"Switching Protocols"),
27  	PROCESSING(102,"Processing"),
28  	OK(200,"OK"),
29  	CREATED(201,"Created"),
30  	ACCEPTED(202,"Accepted"),
31  	NON_AUTHORITATIVE_INFORMATION(203,"Non-Authoritative Information"),
32  	NO_CONTENT(204,"No Content"),
33  	RESET_CONTENT(205,"Reset Content"),
34  	PARTIAL_CONTENT(206,"Partial Content"),
35  	MULTI_STATUS(207,"Multi-Status"),
36  	MULTIPLE_CHIOCES(207,"Multiple-Choices"),
37  	MOVED_PERMANENTLY(301,"Moved Permanently"),
38  	FOUND(302,"Found"),
39  	SEE_OTHER(303,"See Other"),
40  	NOT_MODIFIED(304,"Not Modified"),
41  	USE_PROXY(305,"Use Proxy"),
42  	TEMPORARY_REDIRECT(307,"Temporary Redirect"),
43  	BAD_REQUEST(400,"Bad Request"),
44  	UNAUTHORIZED(401,"Unauthorized"),
45  	PAYMENT_REQUIRED(402,"Payment Required"),
46  	FORBIDDEN(403,"Fobidden"),
47  	ICAP_SERVICE_NOT_FOUND(404,"ICAP Service not found"),
48  	METHOD_NOT_ALLOWED(405,"Method not allowed for service"),
49  	NOT_ACCEPTABLE(406,"Not Acceptable"),
50  	PROXY_AUTHENTICATION_REQUIRED(407,"Proxy Authentication Required"),
51  	REQUEST_TIMEOUT(408,"Request timeout"),
52  	CONFLICT(409,"Conflict"),
53  	GONE(410,"Gone"),
54  	LENGTH_REQUIRED(411,"Length Required"),
55  	PRECONDITION_FAILED(412,"Precondition Failed"),
56  	REQUEST_ENTITY_TOO_LARGE(413,"Request Entity Too Large"),
57  	REQUEST_URI_TOO_LONG(414,"Request-URI Too Long"),
58  	UNSUPPORTED_MEDIA_TYPE(415,"Unsupported Media Type"),
59  	REQUESTED_RANGE_NOT_SATISFIABLE(416,"Requested Range Not Satisfiable"),
60  	EXPECTATION_FAILED(417,"Expectation Failed"),
61  	UNPROCESSABLE_ENTITY(422,"Unprocessable Entity"),
62  	LOCKED(423,"Locked"),
63  	FAILED_DEPENDENCY(424,"Failed Dependency"),
64  	UNORDERED_COLLECTION(425,"Unordered Collection"),
65  	UPGRADE_REQUIRED(426,"Upgrade Required"),
66  	SERVER_ERROR(500,"Server error"),
67  	NOT_IMPLEMENTED(501,"Not Implemented"),
68  	BAD_GATEWAY(502,"Bad Gateway"),
69  	SERVICE_UNAVAILABLE(503,"Service Unavailable"),
70  	GATEWAY_TIMEOUT(504,"Gateway Timeout"),
71  	ICAP_VERSION_NOT_SUPPORTED(505,"ICAP Version Not Supported"),
72  	VARIANT_ALSO_NEGOTIATES(506,"Variant Also Negotiates"),
73  	INSUFFICIENT_STORAGE(507,"Insufficient Storage"),
74  	NOT_EXTENDED(510,"Not Extended");
75  
76  	
77  	private String status;
78  	private int code;
79  	
80  	IcapResponseStatus(int code, String status) {
81  		this.code = code;
82  		this.status = status;
83  	}
84  	
85  	public int getCode() {
86  		return code;
87  	}
88  	
89  	public void toResponseInitialLineValue(ChannelBuffer buffer) {
90  		buffer.writeBytes(Integer.toString(code).getBytes(IcapCodecUtil.ASCII_CHARSET));
91  		buffer.writeByte(IcapCodecUtil.SPACE);
92  		buffer.writeBytes(status.getBytes(IcapCodecUtil.ASCII_CHARSET));
93  	}
94  	
95  	public static IcapResponseStatus fromCode(String code) {
96  		for(IcapResponseStatus status : IcapResponseStatus.values()) {
97  			if(Integer.toString(status.getCode()).equalsIgnoreCase(code)) {
98  				return status;
99  			}
100 		}
101 		throw new IllegalArgumentException("Unknown Icap response code [" + code + "]");
102 	}
103 }