View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12   * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13   * 
14   * 
15   * Change History
16   * Flag       Date        Prog         Description
17   *------------------------------------------------------------------------------- 
18   * 1535756    2006-08-07  lupusalex    Make code warning free
19   * 1565892    2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
20   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
21   * 2204488 	  2008-10-28  raman_arora  Fix code to remove compiler warnings
22   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
23   */
24  
25  package org.metricshub.wbem.sblim.cimclient.internal.http;
26  
27  /*-
28   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
29   * WBEM Java Client
30   * ჻჻჻჻჻჻
31   * Copyright 2023 - 2025 MetricsHub
32   * ჻჻჻჻჻჻
33   * Licensed under the Apache License, Version 2.0 (the "License");
34   * you may not use this file except in compliance with the License.
35   * You may obtain a copy of the License at
36   *
37   *      http://www.apache.org/licenses/LICENSE-2.0
38   *
39   * Unless required by applicable law or agreed to in writing, software
40   * distributed under the License is distributed on an "AS IS" BASIS,
41   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42   * See the License for the specific language governing permissions and
43   * limitations under the License.
44   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
45   */
46  
47  import java.io.IOException;
48  import java.io.InputStream;
49  import java.net.HttpURLConnection;
50  import org.metricshub.wbem.sblim.cimclient.internal.http.io.ASCIIPrintStream;
51  
52  /**
53   * Class HttpServerMethod is responsible for
54   *
55   */
56  public class HttpServerMethod extends HttpMethod {
57  	private String iMethodName;
58  
59  	private String iFile;
60  
61  	private String iProtocol;
62  
63  	private int iMinor, iMajor;
64  
65  	private int iStatus;
66  
67  	private String iReason;
68  
69  	/**
70  	 * Ctor.
71  	 *
72  	 * @param pMajor
73  	 *            Major version
74  	 * @param pMinor
75  	 *            Minor version
76  	 * @param pStatus
77  	 *            Status
78  	 * @param pReason
79  	 *            Reason
80  	 */
81  	public HttpServerMethod(int pMajor, int pMinor, int pStatus, String pReason) {
82  		this.iMinor = pMinor;
83  		this.iMajor = pMajor;
84  		this.iStatus = pStatus;
85  		this.iReason = pReason;
86  	}
87  
88  	/**
89  	 * Ctor.
90  	 *
91  	 * @param pReader
92  	 *            Inputstream
93  	 * @throws IOException
94  	 * @throws HttpException
95  	 */
96  	public HttpServerMethod(InputStream pReader) throws IOException, HttpException {
97  		String line;
98  
99  		do {
100 			line = HttpMethod.readLine(pReader);
101 		} while (line == null || line.length() == 0);
102 		int next = line.indexOf(' ');
103 		int prev = 0;
104 		if (next > -1) {
105 			this.iMethodName = line.substring(0, next).toUpperCase();
106 			if (this.iMethodName.equals("GET") && (line.indexOf(' ', next + 1) == -1)) { // Simple
107 				// request
108 				this.iFile = line.substring(next + 1);
109 			} else { // FullRequest
110 				prev = next + 1;
111 				next = line.indexOf(' ', prev);
112 				this.iFile = line.substring(prev, next);
113 
114 				prev = next + 1;
115 				this.iProtocol = line.substring(prev).toUpperCase();
116 
117 				prev = this.iProtocol.indexOf('/');
118 				next = this.iProtocol.indexOf('.', prev + 1);
119 				try {
120 					this.iMajor = Integer.parseInt(this.iProtocol.substring(prev + 1, next));
121 					this.iMinor = Integer.parseInt(this.iProtocol.substring(next + 1));
122 				} catch (Exception e) {
123 					throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
124 				}
125 			}
126 		} else throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
127 	}
128 
129 	/**
130 	 * Returns the major version
131 	 *
132 	 * @return The major version
133 	 */
134 	public int getMajorVersion() {
135 		return this.iMajor;
136 	}
137 
138 	/**
139 	 * Returns the minor version
140 	 *
141 	 * @return The minor version
142 	 */
143 	public int getMinorVersion() {
144 		return this.iMinor;
145 	}
146 
147 	/**
148 	 * Returns the method name
149 	 *
150 	 * @return The method name
151 	 */
152 	public String getMethodName() {
153 		return this.iMethodName;
154 	}
155 
156 	/**
157 	 * Returns the file
158 	 *
159 	 * @return The file
160 	 */
161 	public String getFile() {
162 		return this.iFile;
163 	}
164 
165 	/**
166 	 * Write to a given output stream
167 	 *
168 	 * @param pStream
169 	 *            the output stream
170 	 */
171 	public void write(ASCIIPrintStream pStream) {
172 		pStream.print("HTTP/" + this.iMajor + "." + this.iMinor + " " + this.iStatus + " " + this.iReason + "\r\n");
173 	}
174 }