4714N/A/*
4714N/A * CDDL HEADER START
4714N/A *
4714N/A * The contents of this file are subject to the terms of the
4714N/A * Common Development and Distribution License, Version 1.0 only
4714N/A * (the "License"). You may not use this file except in compliance
4714N/A * with the License.
4714N/A *
6982N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6982N/A * or http://forgerock.org/license/CDDLv1.0.html.
4714N/A * See the License for the specific language governing permissions
4714N/A * and limitations under the License.
4714N/A *
4714N/A * When distributing Covered Code, include this CDDL HEADER in each
6982N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6982N/A * If applicable, add the following below this CDDL HEADER, with the
6982N/A * fields enclosed by brackets "[]" replaced with your own identifying
6982N/A * information:
4714N/A * Portions Copyright [yyyy] [name of copyright owner]
4714N/A *
4714N/A * CDDL HEADER END
4714N/A *
4714N/A *
4714N/A * Portions Copyright 2009 Sun Microsystems, Inc.
4714N/A */
4714N/A
4714N/Apackage com.ibm.staf.service.opends.tester;
4714N/A
4714N/Aimport java.io.BufferedReader;
4714N/Aimport java.io.ByteArrayInputStream;
4714N/Aimport java.io.Reader;
4714N/Aimport java.io.StringReader;
4714N/Aimport java.util.HashMap;
4714N/Aimport java.util.Iterator;
4714N/Aimport java.util.Map;
4714N/Aimport java.util.StringTokenizer;
4714N/Aimport javax.xml.bind.JAXBContext;
4714N/Aimport javax.xml.bind.JAXBElement;
4714N/Aimport javax.xml.bind.Unmarshaller;
4714N/Aimport javax.xml.soap.MessageFactory;
4714N/Aimport javax.xml.soap.MimeHeaders;
4714N/Aimport javax.xml.soap.SOAPBody;
4714N/Aimport javax.xml.soap.SOAPElement;
4714N/Aimport javax.xml.soap.SOAPMessage;
4714N/Aimport org.opends.dsml.protocol.BatchResponse;
4714N/A
4714N/Apublic class ResponseChecker {
4714N/A
4714N/A private Map<String, String> header = new HashMap<String, String>();
4714N/A private String httpCode;
4714N/A private BatchResponse batchResponse;
4714N/A private String body;
4714N/A public ResponseChecker(String response) throws Exception {
4714N/A this(new StringReader(response));
4714N/A
4714N/A }
4714N/A
4714N/A public ResponseChecker(Reader response) throws Exception {
4714N/A
4714N/A // this.source = new StringBuilder();
4714N/A boolean headerDone = false;
4714N/A String line = null;
4714N/A BufferedReader reader = new BufferedReader(response);
4714N/A StringBuilder sb = new StringBuilder();
4714N/A
4714N/A // assign first line of header to httpCode
4714N/A // then assign all header to <key>:<value> to header map
4714N/A // finally fill sb with body of request
4714N/A while ((line = reader.readLine()) != null) {
4714N/A if (!headerDone) {
4714N/A if (line.length() == 0) {
4714N/A headerDone = true;
4714N/A }
4714N/A if (!headerDone) {
4714N/A if (httpCode == null) {
4714N/A httpCode = line;
4714N/A } else {
4714N/A String[] entry = line.split(":");
4714N/A header.put(entry[0].trim().toLowerCase(), entry[1].trim());
4714N/A }
4714N/A }
4714N/A } else {
4714N/A sb.append(line);
4714N/A }
4714N/A }
4714N/A this.body = sb.toString();
4714N/A
4714N/A if (httpCode != null) {
4714N/A if (httpCode.split(" ")[1].equals("200")) {
4714N/A
4714N/A // reconstruct header for SOAP engine
4714N/A MimeHeaders mimeHeaders = new MimeHeaders();
4714N/A for (Map.Entry<String, String> entry : header.entrySet()) {
4714N/A String name = entry.getKey();
4714N/A String value = entry.getValue();
4714N/A StringTokenizer token = new StringTokenizer(value, ",");
4714N/A while (token.hasMoreTokens()) {
4714N/A mimeHeaders.addHeader(name, token.nextToken().trim());
4714N/A }
4714N/A }
4714N/A MessageFactory messageFactory = MessageFactory.newInstance();
4714N/A SOAPMessage message = messageFactory.createMessage(mimeHeaders, new ByteArrayInputStream(this.body.getBytes()));
4714N/A
4714N/A //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
4714N/A //dbf.setNamespaceAware(true);
4714N/A //DocumentBuilder db = dbf.newDocumentBuilder();
4714N/A //Document doc = db.newDocument();
4714N/A SOAPBody soapBody = message.getSOAPBody();
4714N/A
4714N/A Iterator iterator = soapBody.getChildElements();
4714N/A while (iterator.hasNext()) {
4714N/A Object object = iterator.next();
4714N/A if (!(object instanceof SOAPElement)) {
4714N/A continue;
4714N/A }
4714N/A SOAPElement soapElement = (SOAPElement) object;
4714N/A
4714N/A JAXBContext jaxbContext = JAXBContext.newInstance(BatchResponse.class);
4714N/A Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
4714N/A
4714N/A JAXBElement<BatchResponse> batchResponseElement = unmarshaller.unmarshal(soapElement, BatchResponse.class);
4714N/A
4714N/A this.batchResponse = batchResponseElement.getValue();
4714N/A }
4714N/A }
4714N/A }
4714N/A
4714N/A }
4714N/A
4714N/A public boolean equals(Object o) {
4714N/A if (o == this) {
4714N/A return true;
4714N/A }
4714N/A if (!(o instanceof ResponseChecker)) {
4714N/A return false;
4714N/A }
4714N/A ResponseChecker other = (ResponseChecker) o;
4714N/A
4714N/A // check code "HTTP/1.1 200 OK".split(" ")[1] = "200"
4714N/A if (!this.httpCode.split(" ")[1].equals(other.httpCode.split(" ")[1])) {
4714N/A return false;
4714N/A }
4714N/A
4714N/A if (!Checker.equals(batchResponse, other.batchResponse)) {
4714N/A return false;
4714N/A }
4714N/A
4714N/A return true;
4714N/A }
4714N/A}
4714N/A