View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2009, 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 : Ramandeep S Arora, IBM, arorar@us.ibm.com
12   * 
13   * Flag       Date        Prog         Description
14   * ---------------------------------------------------------------------------
15   * 2860081    2009-09-17  raman_arora  Pull Enumeration Feature (DOM Parser)
16   * 2878054    2009-10-25  raman_arora  Pull Enumeration Feature (PULL Parser)
17   *    2666    2013-09-19  blaschke-oss CR12: Remove ENUMERATIONCONTEXT
18   */
19  
20  package org.metricshub.wbem.sblim.cimclient.internal.wbem;
21  
22  /*-
23   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
24   * WBEM Java Client
25   * ჻჻჻჻჻჻
26   * Copyright 2023 - 2025 MetricsHub
27   * ჻჻჻჻჻჻
28   * Licensed under the Apache License, Version 2.0 (the "License");
29   * you may not use this file except in compliance with the License.
30   * You may obtain a copy of the License at
31   *
32   *      http://www.apache.org/licenses/LICENSE-2.0
33   *
34   * Unless required by applicable law or agreed to in writing, software
35   * distributed under the License is distributed on an "AS IS" BASIS,
36   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37   * See the License for the specific language governing permissions and
38   * limitations under the License.
39   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
40   */
41  
42  import java.io.IOException;
43  import java.io.InputStreamReader;
44  import java.util.List;
45  import javax.xml.parsers.ParserConfigurationException;
46  import org.metricshub.wbem.javax.cim.CIMArgument;
47  import org.metricshub.wbem.javax.cim.CIMObjectPath;
48  import org.metricshub.wbem.javax.wbem.CloseableIterator;
49  import org.metricshub.wbem.javax.wbem.WBEMException;
50  import org.metricshub.wbem.javax.wbem.client.EnumerateResponse;
51  import org.xml.sax.SAXException;
52  
53  /**
54   * Class EnumerateResponseDOM is responsible for all helper functions of DOM
55   * parser related with EnumerateResponse.
56   *
57   * @param <T>
58   *            : Type Variable
59   */
60  public class EnumerateResponseDOM<T> {
61  	private EnumerateResponse<T> enumResponse;
62  
63  	/**
64  	 * EnumerateResponsePULL
65  	 *
66  	 * @param pStream
67  	 * @param pPath
68  	 * @throws IOException
69  	 * @throws SAXException
70  	 * @throws ParserConfigurationException
71  	 * @throws WBEMException
72  	 */
73  	@SuppressWarnings("unchecked")
74  	public EnumerateResponseDOM(InputStreamReader pStream, CIMObjectPath pPath)
75  		throws IOException, SAXException, ParserConfigurationException, WBEMException {
76  		String enumContext = null;
77  
78  		Boolean endOfSequence = null;
79  
80  		CloseableIterator<?> iter = new CloseableIteratorDOM(pStream, pPath);
81  
82  		// check for error and CIMExceptions
83  		try {
84  			iter.hasNext();
85  		} catch (RuntimeException e) {
86  			iter.close();
87  			if (e.getCause() != null && e.getCause() instanceof WBEMException) {
88  				throw (WBEMException) e.getCause();
89  			}
90  			throw e;
91  		}
92  
93  		// get list of output CIMArguments i.e. enumContext and endOfSequence
94  		List<Object> pOutArgA = ((CloseableIteratorDOM) iter).getParamValues();
95  
96  		// pOutArgA can never be null
97  		if (pOutArgA == null) {
98  			throw new IllegalArgumentException("Output auguments not found during CIM-XML DOM parser");
99  		}
100 
101 		for (int i = 0; i < pOutArgA.size(); i++) {
102 			CIMArgument<?> cimArg = (CIMArgument<?>) pOutArgA.get(i);
103 
104 			if (cimArg.getName().equals("EnumerationContext")) enumContext = (String) cimArg.getValue(); else if (
105 				cimArg.getName().equals("EndOfSequence")
106 			) endOfSequence = (Boolean) cimArg.getValue(); else throw new IllegalArgumentException(
107 				"Invalid argument : only EnumerationContext and EndOfSequence are allowed"
108 			);
109 		}
110 		// EndOfSequence can never be null
111 		if (endOfSequence == null) {
112 			throw new IllegalArgumentException("Invalid argument : EndOfSequence can never be null");
113 		}
114 
115 		// EnumerationContext can't be null if there is more data available
116 		if ((endOfSequence.booleanValue() == false) && (enumContext == null)) {
117 			throw new IllegalArgumentException(
118 				"Invalid argument : EnumerationContext cannot be null if there is more data available"
119 			);
120 		}
121 
122 		this.enumResponse =
123 			new EnumerateResponse<T>(enumContext, (CloseableIterator<T>) iter, endOfSequence.booleanValue());
124 	}
125 
126 	/**
127 	 * Returns enumResponse
128 	 *
129 	 * @return The value of enumResponse.
130 	 */
131 	public EnumerateResponse<T> getEnumResponse() {
132 		return this.enumResponse;
133 	}
134 }