001: /*
002: * $Id: UnacceptableMessageException.java,v 1.5 2007/09/18 08:45:07 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.util.Iterator;
010: import java.util.List;
011:
012: import org.xins.common.xml.Element;
013: import org.xins.common.xml.ElementBuilder;
014:
015: /**
016: * Exception that indicates that a request for an API call is considered
017: * unacceptable on the application-level. For example, a mandatory input
018: * parameter may be missing.
019: *
020: * @version $Revision: 1.5 $ $Date: 2007/09/18 08:45:07 $
021: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
022: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
023: *
024: * @since XINS 2.0
025: */
026: public class UnacceptableMessageException extends XINSCallException {
027:
028: // TODO: Support XINSCallRequest objects?
029: // TODO: Is the name UnacceptableRequestException okay?
030:
031: /**
032: * The DataElement containing the errors.
033: */
034: private Element _errors = new Element("data");
035:
036: /**
037: * The error message.
038: */
039: private String _message;
040:
041: /**
042: * Constructs a new <code>UnacceptableMessageException</code> using the
043: * specified <code>AbstractCAPICallRequest</code>.
044: *
045: * @param request
046: * the {@link AbstractCAPICallRequest} that is considered unacceptable,
047: * cannot be <code>null</code>.
048: */
049: UnacceptableMessageException(XINSCallRequest request) {
050: super ("Invalid request", request, null, 0L, null, null);
051: }
052:
053: /**
054: * Constructs a new <code>UnacceptableMessageException</code> using the
055: * specified <code>XINSCallResult</code>.
056: * This constructor is used by the generated CAPI.
057: *
058: * @param result
059: * the {@link XINSCallResult} that is considered unacceptable,
060: * cannot be <code>null</code>.
061: */
062: public UnacceptableMessageException(XINSCallResult result) {
063: super ("Invalid result", result, null, null);
064: }
065:
066: /**
067: * Returns the message for this exception.
068: *
069: * @return
070: * the exception message, can be <code>null</code>.
071: */
072: public String getMessage() {
073: if (_message == null) {
074: _message = InvalidRequestException.createMessage(_errors);
075: }
076: return _message;
077: }
078:
079: /**
080: * Adds to the response that a paramater that is missing.
081: *
082: * @param parameter
083: * the missing parameter.
084: */
085: public void addMissingParameter(String parameter) {
086: ElementBuilder missingParam = new ElementBuilder(
087: "missing-param");
088: missingParam.setAttribute("param", parameter);
089: _errors.addChild(missingParam.createElement());
090: }
091:
092: /**
093: * Adds to the response a parameter that is missing in an element.
094: *
095: * @param parameter
096: * the missing parameter.
097: *
098: * @param element
099: * the element in which the parameter is missing.
100: */
101: public void addMissingParameter(String parameter, String element) {
102: ElementBuilder missingParam = new ElementBuilder(
103: "missing-param");
104: missingParam.setAttribute("param", parameter);
105: missingParam.setAttribute("element", element);
106: _errors.addChild(missingParam.createElement());
107: }
108:
109: /**
110: * Adds an invalid value for a specified type.
111: *
112: * @param parameter
113: * the name of the parameter passed by the user.
114: *
115: * @param type
116: * the type which this parameter should be compliant with.
117: *
118: * @deprecated since XINS 2.0, use {@link #addInvalidValueForType(String, String, String)}.
119: */
120: public void addInvalidValueForType(String parameter, String type) {
121: ElementBuilder invalidValue = new ElementBuilder(
122: "invalid-value-for-type");
123: invalidValue.setAttribute("param", parameter);
124: invalidValue.setAttribute("type", type);
125: _errors.addChild(invalidValue.createElement());
126: }
127:
128: /**
129: * Adds an invalid value for a specified type.
130: *
131: * @param parameter
132: * the name of the parameter passed by the user.
133: *
134: * @param value
135: * the value of the parameter passed by the user.
136: *
137: * @param type
138: * the type which this parameter should be compliant with.
139: *
140: * @since XINS 2.0
141: */
142: public void addInvalidValueForType(String parameter, String value,
143: String type) {
144: ElementBuilder invalidValue = new ElementBuilder(
145: "invalid-value-for-type");
146: invalidValue.setAttribute("param", parameter);
147: invalidValue.setAttribute("value", value);
148: invalidValue.setAttribute("type", type);
149: _errors.addChild(invalidValue.createElement());
150: }
151:
152: /**
153: * Adds an invalid value for a specified type.
154: *
155: * @param parameter
156: * the parameter passed by the user.
157: *
158: * @param value
159: * the value of the parameter passed by the user.
160: *
161: * @param type
162: * the type which this parameter should be compliant with.
163: *
164: * @param element
165: * the element in which the parameter is missing.
166: *
167: * @since XINS 2.0
168: */
169: public void addInvalidValueForType(String parameter, String value,
170: String type, String element) {
171: ElementBuilder invalidValue = new ElementBuilder(
172: "invalid-value-for-type");
173: invalidValue.setAttribute("param", parameter);
174: invalidValue.setAttribute("value", value);
175: invalidValue.setAttribute("type", type);
176: invalidValue.setAttribute("element", element);
177: _errors.addChild(invalidValue.createElement());
178: }
179:
180: /**
181: * Adds an invalid combination of parameters.
182: *
183: * @param type
184: * the type of the combination.
185: *
186: * @param parameters
187: * list of the parameters in the combination passed as a list of
188: * {@link String} objects.
189: */
190: public void addParamCombo(String type, List parameters) {
191:
192: ElementBuilder paramCombo = new ElementBuilder("param-combo");
193: paramCombo.setAttribute("type", type);
194:
195: // Iterate over all parameters
196: Iterator itParameters = parameters.iterator();
197: while (itParameters.hasNext()) {
198: ElementBuilder param = new ElementBuilder("param");
199: param.setAttribute("name", (String) itParameters.next());
200: paramCombo.addChild(param.createElement());
201: }
202:
203: _errors.addChild(paramCombo.createElement());
204: }
205:
206: /**
207: * Adds an invalid combination of attributes.
208: *
209: * @param type
210: * the type of the combination.
211: *
212: * @param attributes
213: * list of the attributes in the combination passed as a list of
214: * {@link String} objects.
215: *
216: * @param elementName
217: * the name of the element to which these attributes belong.
218: *
219: * @since XINS 1.4.0
220: */
221: public void addAttributeCombo(String type, List attributes,
222: String elementName) {
223:
224: ElementBuilder attributeCombo = new ElementBuilder(
225: "attribute-combo");
226: attributeCombo.setAttribute("type", type);
227:
228: // Iterate over all attributes
229: Iterator itAttributes = attributes.iterator();
230: while (itAttributes.hasNext()) {
231: ElementBuilder attribute = new ElementBuilder("attribute");
232: attribute
233: .setAttribute("name", (String) itAttributes.next());
234: attributeCombo.addChild(attribute.createElement());
235: }
236:
237: _errors.addChild(attributeCombo.createElement());
238: }
239: }
|