org.glassfish.admingui.common.util.RestResponse Maven / Gradle / Ivy
Show all versions of payara-embedded-all Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.admingui.common.util;
import org.w3c.dom.*;
import javax.ws.rs.core.Response;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
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;
/**
* 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.newInstance();
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