001/* 002 * Copyright (c) 2012, the Last.fm Java Project and Committers 003 * All rights reserved. 004 * 005 * Redistribution and use of this software in source and binary forms, with or without modification, are 006 * permitted provided that the following conditions are met: 007 * 008 * - Redistributions of source code must retain the above 009 * copyright notice, this list of conditions and the 010 * following disclaimer. 011 * 012 * - Redistributions in binary form must reproduce the above 013 * copyright notice, this list of conditions and the 014 * following disclaimer in the documentation and/or other 015 * materials provided with the distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 019 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 020 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 021 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 023 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 025 */ 026package de.umass.lastfm; 027 028import org.w3c.dom.Document; 029 030import de.umass.xml.DomElement; 031 032/** 033 * The <code>Result</code> class contains the response sent by the server, i.e. the status (either ok or failed), 034 * an error code and message if failed and the xml response sent by the server. 035 * 036 * @author Janni Kovacs 037 */ 038public class Result { 039 040 public enum Status { 041 OK, 042 FAILED 043 } 044 045 protected Status status; 046 protected String errorMessage = null; 047 protected int errorCode = -1; 048 protected int httpErrorCode = -1; 049 050 protected Document resultDocument; 051 052 protected Result(Document resultDocument) { 053 this.status = Status.OK; 054 this.resultDocument = resultDocument; 055 } 056 057 protected Result(String errorMessage) { 058 this.status = Status.FAILED; 059 this.errorMessage = errorMessage; 060 } 061 062 static Result createOkResult(Document resultDocument) { 063 return new Result(resultDocument); 064 } 065 066 static Result createHttpErrorResult(int httpErrorCode, String errorMessage) { 067 Result r = new Result(errorMessage); 068 r.httpErrorCode = httpErrorCode; 069 return r; 070 } 071 072 static Result createRestErrorResult(int errorCode, String errorMessage) { 073 Result r = new Result(errorMessage); 074 r.errorCode = errorCode; 075 return r; 076 } 077 078 /** 079 * Returns if the operation was successful. Same as <code>getStatus() == Status.OK</code>. 080 * 081 * @return <code>true</code> if the operation was successful 082 */ 083 public boolean isSuccessful() { 084 return status == Status.OK; 085 } 086 087 public int getErrorCode() { 088 return errorCode; 089 } 090 091 public int getHttpErrorCode() { 092 return httpErrorCode; 093 } 094 095 public Status getStatus() { 096 return status; 097 } 098 099 public Document getResultDocument() { 100 return resultDocument; 101 } 102 103 public String getErrorMessage() { 104 return errorMessage; 105 } 106 107 public DomElement getContentElement() { 108 if (!isSuccessful()) 109 return null; 110 return new DomElement(resultDocument.getDocumentElement()).getChild("*"); 111 } 112 113 @Override 114 public String toString() { 115 return "Result[isSuccessful=" + isSuccessful() + ", errorCode=" + errorCode + ", httpErrorCode=" + httpErrorCode + ", errorMessage=" 116 + errorMessage + ", status=" + status+"]"; 117 } 118}