001: /*
002: * $Id: InvalidResultXINSCallException.java,v 1.18 2007/03/16 09:54:58 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.client;
008:
009: import java.io.UnsupportedEncodingException;
010:
011: import org.xins.common.MandatoryArgumentChecker;
012: import org.xins.common.Utils;
013: import org.xins.common.service.TargetDescriptor;
014:
015: /**
016: * Exception thrown to indicate that the result from a XINS API call was
017: * invalid according to the XINS rules for a XINS call result.
018: *
019: * <p>Note that this exception is <em>only</em> thrown if the result is
020: * invalid according to the XINS rules for a result XML document. If the
021: * result is only invalid in relation to the applicable API specification,
022: * then an {@link UnacceptableResultXINSCallException} is thrown instead.
023: *
024: * @version $Revision: 1.18 $ $Date: 2007/03/16 09:54:58 $
025: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
026: *
027: * @since XINS 1.0.0
028: */
029: public final class InvalidResultXINSCallException extends
030: XINSCallException {
031:
032: /**
033: * Constructs a new <code>InvalidResultXINSCallException</code>.
034: *
035: * @param request
036: * the original request, cannot be <code>null</code>.
037: *
038: * @param target
039: * descriptor for the target that was attempted to be called, cannot be
040: * <code>null</code>.
041: *
042: * @param duration
043: * the call duration in milliseconds, must be >= 0.
044: *
045: * @param detail
046: * a more detailed description of the problem, or <code>null</code> if
047: * none is available.
048: *
049: * @param cause
050: * the cause exception, or <code>null</code> if there is none.
051: *
052: * @throws IllegalArgumentException
053: * if <code>request == null
054: * || target == null
055: * || duration < 0</code>.
056: */
057: private InvalidResultXINSCallException(XINSCallRequest request,
058: TargetDescriptor target, long duration, String detail,
059: Throwable cause) throws IllegalArgumentException {
060: super ("Invalid XINS call result", request, target, duration,
061: detail, cause);
062: }
063:
064: /**
065: * Creates a <code>InvalidResultXINSCallException</code> for the situation
066: * where no HTTP data is received.
067: *
068: * @param request
069: * the original request, cannot be <code>null</code>.
070: *
071: * @param target
072: * descriptor for the target that was attempted to be called, cannot be
073: * <code>null</code>.
074: *
075: * @param duration
076: * the call duration in milliseconds, must be >= 0.
077: *
078: * @return
079: * the exception indicating that no HTTP data was received.
080: *
081: * @throws IllegalArgumentException
082: * if <code>request == null
083: * || target == null
084: * || duration < 0</code>.
085: */
086: static InvalidResultXINSCallException noDataReceived(
087: XINSCallRequest request, TargetDescriptor target,
088: long duration) throws IllegalArgumentException {
089:
090: // Check preconditions
091: MandatoryArgumentChecker.check("request", request, "target",
092: target);
093: if (duration < 0) {
094: throw new IllegalArgumentException("duration (" + duration
095: + ") < 0");
096: }
097:
098: String detail = "No HTTP response received.";
099: Throwable cause = null;
100:
101: return new InvalidResultXINSCallException(request, target,
102: duration, detail, cause);
103: }
104:
105: /**
106: * Creates a <code>InvalidResultXINSCallException</code> for the situation
107: * where the received HTTP data cannot be parsed.
108: *
109: * @param httpData
110: * the HTTP data, cannot be <code>null</code> and the length must be
111: * positive.
112: *
113: * @param request
114: * the original request, cannot be <code>null</code>.
115: *
116: * @param target
117: * descriptor for the target that was attempted to be called, cannot be
118: * <code>null</code>.
119: *
120: * @param duration
121: * the call duration in milliseconds, must be >= 0.
122: *
123: * @param cause
124: * the cause exception, or <code>null</code> if there is none.
125: *
126: * @return
127: * the exception indicating that the HTTP data could not be parsed.
128: *
129: * @throws IllegalArgumentException
130: * if <code>httpData == null
131: * || request == null
132: * || target == null
133: * || httpData.length < 1
134: * || duration < 0</code>.
135: */
136: static InvalidResultXINSCallException parseError(byte[] httpData,
137: XINSCallRequest request, TargetDescriptor target,
138: long duration, Throwable cause)
139: throws IllegalArgumentException {
140:
141: // Check preconditions
142: MandatoryArgumentChecker.check("httpData", httpData, "request",
143: request, "target", target);
144: if (httpData.length < 1) {
145: throw new IllegalArgumentException("httpData.length == 0");
146: } else if (duration < 0) {
147: throw new IllegalArgumentException("duration (" + duration
148: + ") < 0");
149: }
150:
151: // Determine how much to quote; max is 512 bytes
152: int quoteLength = Math.min(httpData.length, 512);
153: String quote;
154: try {
155: quote = new String(httpData, 0, quoteLength, "US-ASCII");
156: } catch (UnsupportedEncodingException exception) {
157: throw Utils.logProgrammingError(cause);
158: }
159:
160: // Construct the detail message
161: String detail = "Failed to parse the HTTP response. The first "
162: + quoteLength + " bytes are (in ASCII): \"" + quote
163: + "\".";
164:
165: return new InvalidResultXINSCallException(request, target,
166: duration, detail, cause);
167: }
168: }
|