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   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
24   */
25  
26  package org.metricshub.wbem.sblim.cimclient.internal.http;
27  
28  /*-
29   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
30   * WBEM Java Client
31   * ჻჻჻჻჻჻
32   * Copyright 2023 - 2025 MetricsHub
33   * ჻჻჻჻჻჻
34   * Licensed under the Apache License, Version 2.0 (the "License");
35   * you may not use this file except in compliance with the License.
36   * You may obtain a copy of the License at
37   *
38   *      http://www.apache.org/licenses/LICENSE-2.0
39   *
40   * Unless required by applicable law or agreed to in writing, software
41   * distributed under the License is distributed on an "AS IS" BASIS,
42   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43   * See the License for the specific language governing permissions and
44   * limitations under the License.
45   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
46   */
47  
48  import java.io.IOException;
49  import java.io.InputStream;
50  import java.net.HttpURLConnection;
51  import org.metricshub.wbem.sblim.cimclient.internal.http.io.ASCIIPrintStream;
52  
53  /**
54   * Class HttpClientMethod encapsulates a http client method
55   *
56   */
57  public class HttpClientMethod extends HttpMethod {
58  	private boolean iIncomming = true;
59  
60  	private String iHttpHeader;
61  
62  	private int iMinor, iMajor;
63  
64  	private int iStatus;
65  
66  	private String iMethod;
67  
68  	private String iRequest;
69  
70  	private String iResponse;
71  
72  	/**
73  	 * Ctor. Creates an outgoing http method
74  	 *
75  	 * @param pMethod
76  	 *            The method
77  	 * @param pRequest
78  	 *            The request
79  	 * @param pMajor
80  	 *            The major version
81  	 * @param pMinor
82  	 *            The minor version
83  	 */
84  	public HttpClientMethod(String pMethod, String pRequest, int pMajor, int pMinor) {
85  		this.iMinor = pMinor;
86  		this.iMajor = pMajor;
87  		this.iRequest = pRequest;
88  		this.iMethod = pMethod;
89  		this.iIncomming = false;
90  	}
91  
92  	/**
93  	 * Ctor. Parses an incoming http method from a given input stream
94  	 *
95  	 * @param pReader
96  	 *            The input stream
97  	 * @throws IOException
98  	 */
99  	public HttpClientMethod(InputStream pReader) throws IOException {
100 		String line = null;
101 
102 		do {
103 			line = readLine(pReader);
104 		} while (line == null || line.length() == 0);
105 		int rqt = line.indexOf(' ');
106 		int prev = 0;
107 		if (rqt > -1) { // Parse the header
108 			int next = rqt;
109 			this.iHttpHeader = line.substring(prev, next).toUpperCase();
110 
111 			prev = this.iHttpHeader.indexOf('/');
112 			if (prev > 0 && this.iHttpHeader.substring(0, prev).equalsIgnoreCase("HTTP")) {
113 				next = this.iHttpHeader.indexOf('.', prev + 1);
114 				try {
115 					this.iMajor = Integer.parseInt(this.iHttpHeader.substring(prev + 1, next));
116 					this.iMinor = Integer.parseInt(this.iHttpHeader.substring(next + 1));
117 				} catch (Exception e) {
118 					throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
119 				}
120 				prev = rqt;
121 				rqt = line.indexOf(' ', prev + 1);
122 				if (rqt > -1) {
123 					try {
124 						this.iStatus = Integer.parseInt(line.substring(prev + 1, rqt));
125 					} catch (Exception e) {
126 						throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
127 					}
128 					this.iResponse = line.substring(rqt + 1);
129 					return;
130 				}
131 			} else throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
132 		}
133 		throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
134 	}
135 
136 	/**
137 	 * Returns the major version
138 	 *
139 	 * @return The major version
140 	 */
141 	public int getMajorVersion() {
142 		return this.iMajor;
143 	}
144 
145 	/**
146 	 * Returns the minor version
147 	 *
148 	 * @return The minor version
149 	 */
150 	public int getMinorVersion() {
151 		return this.iMinor;
152 	}
153 
154 	/**
155 	 * Returns the method name
156 	 *
157 	 * @return The method name
158 	 */
159 	public String getMethodName() {
160 		return this.iMethod;
161 	}
162 
163 	/**
164 	 * Return the status
165 	 *
166 	 * @return The status
167 	 */
168 	public int getStatus() {
169 		return this.iStatus;
170 	}
171 
172 	/**
173 	 * Writes the method to a given stream
174 	 *
175 	 * @param pStream
176 	 *            The stream
177 	 */
178 	public void write(ASCIIPrintStream pStream) {
179 		pStream.print(this.iMethod + " " + this.iRequest + " HTTP/" + this.iMajor + "." + this.iMinor + "\r\n");
180 	}
181 
182 	/**
183 	 * Returns the response message
184 	 *
185 	 * @return The response message
186 	 */
187 	public String getResponseMessage() {
188 		return this.iResponse;
189 	}
190 
191 	@Override
192 	public String toString() {
193 		if (this.iIncomming) {
194 			return this.iHttpHeader + " " + this.iStatus + " " + this.iResponse;
195 		}
196 		return this.iMethod + " " + this.iRequest + " HTTP/" + this.iMajor + "." + this.iMinor;
197 	}
198 }