
org.glassfish.admingui.common.util.RestResponse Maven / Gradle / Ivy
Show all versions of console-common Show documentation
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.admingui.common.util;
import jakarta.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* This class abstracts the response from the admin console code so that we can use JSON / REST interchangeably.
*
*
* @author jasonlee
* @author Ken Paulsen ([email protected])
*/
public abstract class RestResponse {
public abstract int getResponseCode();
public abstract String getResponseBody();
public static RestResponse getRestResponse(Response response) {
return new JerseyRestResponse(response);
}
public boolean isSuccess() {
int status = getResponseCode();
return (status >= 200) && (status <= 299);
}
/**
*
* This method abstracts the physical response to return a consistent data structure. For many responses, this data
* structure may look like:
*
*
*
*
* Map<String, Object>
* {
* "responseCode" : Integer // HTTP Response code, ie. 200
* "output" : String // The Raw Response Body
* "description" : String // Command Description
* // 0 or more messages returned from the command
* "messages" : List<Map<String, Object>>
* [
* {
* "message" : String // Raw Message String
* "..." : String // Additional custom attributes
* // List of properties for this message
* "properties" : List<Map<String, Object>>
* [
* {
* "name" : String // The Property Name
* "value" : String // The Property Value
* "properties" : List // Child Properties
* }, ...
* ]
* }, ...
* ]
* }
*
*
*/
public abstract Map getResponse();
public abstract void close();
}
class JerseyRestResponse extends RestResponse {
protected Response response;
private String body = null;
public JerseyRestResponse(Response response) {
this.response = response;
}
@Override
public String getResponseBody() {
if (body == null) {
body = response.readEntity(String.class);
}
return body;
}
@Override
public int getResponseCode() {
return response.getStatus();
}
/**
*
* This method abstracts the physical response to return a consistent data structure.
*
*/
@Override
public Map getResponse() {
// Prepare the result object
Map result = new HashMap<>(5);
// Add the Response Code
result.put("responseCode", getResponseCode());
// Add the Response Body
// FIXME: Do not put responseBody into the Map... too big, not needed
result.put("responseBody", getResponseBody());
String contentType = response.getHeaderString("Content-type");
if (contentType != null) {
String responseBody = getResponseBody();
contentType = contentType.toLowerCase(GuiUtil.guiLocale);
if (contentType.startsWith("application/xml")) {
InputStream input = null;
try {
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
input = new ByteArrayInputStream(responseBody.trim().getBytes("UTF-8"));
XMLStreamReader parser = inputFactory.createXMLStreamReader(input);
while (parser.hasNext()) {
int event = parser.next();
switch (event) {
case XMLStreamConstants.START_ELEMENT: {
if ("map".equals(parser.getLocalName())) {
result.put("data", processXmlMap(parser));
}
break;
}
default:
break;
}
}
} catch (Exception ex) {
Logger.getLogger(RestResponse.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ex) {
Logger.getLogger(RestResponse.class.getName()).log(Level.SEVERE, null, ex);
}
}
// // If XML...
// Document document = MiscUtil.getDocument(getResponseBody());
// Element root = document.getDocumentElement();
// if ("action-report".equalsIgnoreCase(root.getNodeName())) {
// // Default XML document type...
// // Add the Command Description
// result.put("description", root.getAttribute("description"));
// result.put("exit-code", root.getAttribute("exit-code"));
//
// // Add the messages
// List