|
Glassfish example source code file (RestResponse.java)
This example Glassfish source code file (RestResponse.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Glassfish RestResponse.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010 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 com.sun.jersey.api.client.ClientResponse;
import org.glassfish.admin.rest.clientutils.MarshallingUtils;
import org.w3c.dom.*;
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;
/**
* <p> This class abstracts the response from the admin console code so that
* we can use JSON / REST interchangeably.</p>
*
* @author jasonlee
* @author Ken Paulsen (ken.paulsen@oracle.com)
*/
public abstract class RestResponse {
public abstract int getResponseCode();
public abstract String getResponseBody();
public static RestResponse getRestResponse(ClientResponse response) {
return new JerseyRestResponse(response);
}
public boolean isSuccess() {
int status = getResponseCode();
return (status >= 200) && (status <= 299);
}
/**
* <p> This method abstracts the physical response to return a consistent
* data structure. For many responses, this data structure may look
* like:</p>
* <p/>
* <p>
* <code>
* 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
* }, ...
* ]
* }, ...
* ]
* }
* </code>
* </p>
*/
public abstract Map<String, Object> getResponse();
}
class JerseyRestResponse extends RestResponse {
protected ClientResponse response;
private String body = null;
public JerseyRestResponse(ClientResponse response) {
this.response = response;
}
@Override
public String getResponseBody() {
if (body == null) {
body = response.getEntity(String.class);
}
return body;
}
@Override
public int getResponseCode() {
return response.getStatus();
}
/**
* <p> This method abstracts the physical response to return a consistent
* data structure.</p>
*/
@Override
public Map<String, Object> getResponse() {
// Prepare the result object
Map<String, Object> 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.getHeaders().getFirst("Content-type");
if (contentType != null) {
String responseBody = getResponseBody();
contentType = contentType.toLowerCase();
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;
}
}
}
} catch (Exception ex) {
Logger.getLogger(RestResponse.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
} finally {
try {
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<Map messages = new ArrayList
Other Glassfish examples (source code examples)
Here is a short list of links related to this Glassfish RestResponse.java source code file:
|