mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Christophe Sovant
11.48.2009 0026d9becf489657e300fb2b65e67287475c7060
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/proctor/resource/legal-notices/Proctor.LICENSE
 * or https://proctor.dev.java.net/Proctor.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2009 Sun Microsystems, Inc.
 */
 
package com.ibm.staf.service.opends.tester;
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;
import org.opends.dsml.protocol.BatchResponse;
 
public class ResponseChecker {
 
    private Map<String, String> header = new HashMap<String, String>();
    private String httpCode;
    private BatchResponse batchResponse;
    private String body;
    public ResponseChecker(String response) throws Exception {
        this(new StringReader(response));
 
    }
 
    public ResponseChecker(Reader response) throws Exception {
 
        // this.source = new StringBuilder();
        boolean headerDone = false;
        String line = null;
        BufferedReader reader = new BufferedReader(response);
        StringBuilder sb = new StringBuilder();
 
        // assign first line of header to httpCode
        // then assign all header to <key>:<value> to header map
        // finally fill sb with body of request
        while ((line = reader.readLine()) != null) {
            if (!headerDone) {
                if (line.length() == 0) {
                    headerDone = true;
                }
                if (!headerDone) {
                    if (httpCode == null) {
                        httpCode = line;
                    } else {
                        String[] entry = line.split(":");
                        header.put(entry[0].trim().toLowerCase(), entry[1].trim());
                    }
                }
            } else {
                sb.append(line);
            }
        }
        this.body = sb.toString();
 
        if (httpCode != null) {
            if (httpCode.split(" ")[1].equals("200")) {
 
                // reconstruct header for SOAP engine
                MimeHeaders mimeHeaders = new MimeHeaders();
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    String name = entry.getKey();
                    String value = entry.getValue();
                    StringTokenizer token = new StringTokenizer(value, ",");
                    while (token.hasMoreTokens()) {
                        mimeHeaders.addHeader(name, token.nextToken().trim());
                    }
                }
                MessageFactory messageFactory = MessageFactory.newInstance();
                SOAPMessage message = messageFactory.createMessage(mimeHeaders, new ByteArrayInputStream(this.body.getBytes()));
 
                //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                //dbf.setNamespaceAware(true);
                //DocumentBuilder db = dbf.newDocumentBuilder();
                //Document doc = db.newDocument();
                SOAPBody soapBody = message.getSOAPBody();
 
                Iterator iterator = soapBody.getChildElements();
                while (iterator.hasNext()) {
                    Object object = iterator.next();
                    if (!(object instanceof SOAPElement)) {
                        continue;
                    }
                    SOAPElement soapElement = (SOAPElement) object;
 
                    JAXBContext jaxbContext = JAXBContext.newInstance(BatchResponse.class);
                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
 
                    JAXBElement<BatchResponse> batchResponseElement = unmarshaller.unmarshal(soapElement, BatchResponse.class);
 
                    this.batchResponse = batchResponseElement.getValue();
                }
            }
        }
 
    }
   
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof ResponseChecker)) {
            return false;
        }
        ResponseChecker other = (ResponseChecker) o;
 
        // check code "HTTP/1.1 200 OK".split(" ")[1] = "200"
        if (!this.httpCode.split(" ")[1].equals(other.httpCode.split(" ")[1])) {
            return false;
        }
 
        if (!Checker.equals(batchResponse, other.batchResponse)) {
            return false;
        }
 
        return true;
    }
}