View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2013
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   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
22   * 3062747    2010-09-21  blaschke-oss SblimCIMClient does not log all CIM-XML responces.
23   * 3185833    2011-02-18  blaschke-oss missing newline when logging request/response
24   * 3554738    2012-08-16  blaschke-oss dump CIM xml by LogAndTraceBroker.trace()
25   * 3601894    2013-01-23  blaschke-oss Enhance HTTP and CIM-XML tracing
26   */
27  package org.metricshub.wbem.sblim.cimclient.internal.http.io;
28  
29  /*-
30   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31   * WBEM Java Client
32   * ჻჻჻჻჻჻
33   * Copyright 2023 - 2025 MetricsHub
34   * ჻჻჻჻჻჻
35   * Licensed under the Apache License, Version 2.0 (the "License");
36   * you may not use this file except in compliance with the License.
37   * You may obtain a copy of the License at
38   *
39   *      http://www.apache.org/licenses/LICENSE-2.0
40   *
41   * Unless required by applicable law or agreed to in writing, software
42   * distributed under the License is distributed on an "AS IS" BASIS,
43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44   * See the License for the specific language governing permissions and
45   * limitations under the License.
46   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
47   */
48  
49  import java.io.FilterInputStream;
50  import java.io.IOException;
51  import java.io.InputStream;
52  import java.io.OutputStream;
53  import java.util.logging.Level;
54  import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
55  import org.metricshub.wbem.sblim.cimclient.internal.logging.TimeStamp;
56  
57  /**
58   * Class DebugInputStream is for debugging purposes
59   *
60   */
61  public class DebugInputStream extends FilterInputStream {
62  	private byte[] iBuf;
63  
64  	private boolean iBuffered;
65  
66  	private int iCur = 0;
67  
68  	private int iMaxLen = 0;
69  
70  	private OutputStream iStream;
71  
72  	private String iOrigin;
73  
74  	/**
75  	 * Ctor.
76  	 *
77  	 * @param is
78  	 * @param os
79  	 */
80  	public DebugInputStream(InputStream is, OutputStream os) {
81  		this(is, os, null);
82  	}
83  
84  	/**
85  	 * Ctor.
86  	 *
87  	 * @param is
88  	 * @param os
89  	 * @param pOrigin
90  	 */
91  	public DebugInputStream(InputStream is, OutputStream os, String pOrigin) {
92  		super(is);
93  		this.iBuf = new byte[512];
94  		this.iBuffered = false;
95  		this.iStream = os;
96  		this.iOrigin = pOrigin == null ? "unknown" : pOrigin;
97  	}
98  
99  	private void buffer() throws IOException {
100 		this.iBuffered = true;
101 		int total;
102 		try {
103 			while ((total = this.in.read(this.iBuf, this.iMaxLen, this.iBuf.length - this.iMaxLen)) > -1) {
104 				this.iMaxLen += total;
105 				if (this.iMaxLen == this.iBuf.length) {
106 					byte b[] = new byte[this.iBuf.length << 1];
107 					System.arraycopy(this.iBuf, 0, b, 0, this.iBuf.length);
108 					this.iBuf = b;
109 				}
110 			}
111 		} catch (TrailerException e) {
112 			// TrailerException indicates complete response BUT error in trailer
113 			writeBuffer(this.iOrigin + " begin (TrailerException occurred)");
114 			throw e;
115 		}
116 		writeBuffer(this.iOrigin + " begin");
117 	}
118 
119 	private void writeBuffer(String header) throws IOException {
120 		StringBuilder outStr = new StringBuilder("<--- ");
121 		outStr.append(header);
122 		outStr.append(' ');
123 		outStr.append(TimeStamp.formatWithMillis(System.currentTimeMillis()));
124 		outStr.append(" ----\n");
125 		outStr.append(new String(this.iBuf, 0, this.iMaxLen));
126 		if (this.iMaxLen > 0 && this.iBuf[this.iMaxLen - 1] != '\n') outStr.append('\n');
127 		outStr.append("---- ");
128 		outStr.append(this.iOrigin);
129 		outStr.append(" end ----->\n");
130 		if (this.iStream != null) this.iStream.write(outStr.toString().getBytes());
131 		if (LogAndTraceBroker.getBroker().isLoggableCIMXMLTrace(Level.FINEST)) LogAndTraceBroker
132 			.getBroker()
133 			.traceCIMXML(Level.FINEST, outStr.toString(), false);
134 	}
135 
136 	@Override
137 	public synchronized int read() throws IOException {
138 		if (!this.iBuffered) buffer();
139 
140 		if (this.iCur >= this.iMaxLen) return -1;
141 		return this.iBuf[this.iCur++];
142 	}
143 
144 	@Override
145 	public synchronized int read(byte b[], int off, int len) throws IOException {
146 		if (b == null) {
147 			throw new NullPointerException();
148 		} else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
149 			throw new IndexOutOfBoundsException();
150 		} else if (len == 0) {
151 			return 0;
152 		}
153 
154 		int c = read();
155 		if (c == -1) {
156 			return -1;
157 		}
158 		b[off] = (byte) c;
159 
160 		int i = 1;
161 		for (; i < len; i++) {
162 			c = read();
163 			if (c == -1) {
164 				break;
165 			}
166 			if (b != null) {
167 				b[off + i] = (byte) c;
168 			}
169 		}
170 		return i;
171 	}
172 }