From c194f7d215935cded5b2eab4daeed725a5a4069c Mon Sep 17 00:00:00 2001 From: Gurmeen Bindra Date: Thu, 29 Mar 2012 15:34:35 +0100 Subject: Port Error Parser to stroke After this change, the error payload object should be populated in case of error. The condtion, type and text field will be from the payload rather than Undefined, Cancel and empty. Test-information: tested by executing adhoc-commands on an XMPP clinet in a way to result in an error. I do see the error text and condition set as per the XMPP streams. Reviewer: Kevin Smith diff --git a/src/com/isode/stroke/elements/ErrorPayload.java b/src/com/isode/stroke/elements/ErrorPayload.java index b0ffd58..42e9c61 100644 --- a/src/com/isode/stroke/elements/ErrorPayload.java +++ b/src/com/isode/stroke/elements/ErrorPayload.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, Isode Limited, London, England. + * Copyright (c) 2010-2012, Isode Limited, London, England. * All rights reserved. */ /* @@ -16,6 +16,7 @@ public class ErrorPayload extends Payload { private Condition condition_; private Type type_; private String text_; + private Payload payload_; public enum Type { Cancel, Continue, Modify, Auth, Wait }; @@ -85,4 +86,12 @@ public class ErrorPayload extends Payload { public String getText() { return text_; } + + public void setPayload(Payload payload) { + this.payload_ = payload; + } + + public Payload getPayload() { + return payload_; + } } diff --git a/src/com/isode/stroke/parser/payloadparsers/ErrorParser.java b/src/com/isode/stroke/parser/payloadparsers/ErrorParser.java new file mode 100644 index 0000000..7ff0b49 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/ErrorParser.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.parser.PayloadParserFactoryCollection; + +/** + * Class that represents a parser for the Error payload + * + */ +public class ErrorParser extends GenericPayloadParser { + private enum Level { + TopLevel(0), + PayloadLevel(1); + private Level(int val) { + this.value = val; + } + int value; + } + private PayloadParserFactoryCollection factories_; + private int level_; + private String currentText_ = ""; + private PayloadParser currentPayloadParser; + + /** + * Create the parse + * @param factories reference to the Payload parser factory, + * should not be null + */ + public ErrorParser(PayloadParserFactoryCollection factories) { + super(new ErrorPayload()); + level_ = Level.TopLevel.value; + this.factories_ = factories; + } + + @Override + public void handleStartElement(String element, String ns, final AttributeMap attributes) { + if (level_ == Level.TopLevel.value) { + String type = attributes.getAttribute("type"); + if (type.equals("continue")) { + getPayloadInternal().setType(ErrorPayload.Type.Continue); + } else if (type.equals("modify")) { + getPayloadInternal().setType(ErrorPayload.Type.Modify); + }else if (type.equals("auth")) { + getPayloadInternal().setType(ErrorPayload.Type.Auth); + }else if (type.equals("wait")) { + getPayloadInternal().setType(ErrorPayload.Type.Wait); + }else { + getPayloadInternal().setType(ErrorPayload.Type.Cancel); + } + }else if (level_ == Level.PayloadLevel.value) { + if (element.equals("text")) { + + }else if (element.equals("bad-request")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.BadRequest); + }else if (element.equals("conflict")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.Conflict); + }else if (element.equals("feature-not-implemented")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.FeatureNotImplemented); + }else if (element.equals("forbidden")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.Forbidden); + }else if (element.equals("gone")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.Gone); + }else if (element.equals("internal-server-error")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.InternalServerError); + }else if (element.equals("item-not-found")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.ItemNotFound); + }else if (element.equals("jid-malformed")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.JIDMalformed); + }else if (element.equals("not-acceptable")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.NotAcceptable); + }else if (element.equals("not-allowed")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.NotAllowed); + }else if (element.equals("not-authorized")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.NotAuthorized); + }else if (element.equals("payment-required")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.PaymentRequired); + }else if (element.equals("recipient-unavailable")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.RecipientUnavailable); + }else if (element.equals("redirect")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.Redirect); + }else if (element.equals("registration-required")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.RegistrationRequired); + }else if (element.equals("remote-server-not-found")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.RemoteServerNotFound); + }else if (element.equals("remote-server-timeout")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.RemoteServerTimeout); + }else if (element.equals("resource-constraint")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.ResourceConstraint); + }else if (element.equals("service-unavailable")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.ServiceUnavailable); + }else if (element.equals("subscription-required")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.SubscriptionRequired); + }else if (element.equals("unexpected-request")) { + getPayloadInternal().setCondition(ErrorPayload.Condition.UnexpectedRequest); + }else { + PayloadParserFactory payloadParserFactory = factories_.getPayloadParserFactory(element, ns, attributes); + if (payloadParserFactory != null) { + currentPayloadParser = payloadParserFactory.createPayloadParser(); + } else { + getPayloadInternal().setCondition(ErrorPayload.Condition.UndefinedCondition); + } + } + } + if (level_ >= Level.PayloadLevel.value && currentPayloadParser != null) { + currentPayloadParser.handleStartElement(element, ns, attributes); + } + ++level_; + } + + @Override + public void handleEndElement(final String element, final String ns) { + --level_; + ErrorPayload payloadInternal = getPayloadInternal(); + if (currentPayloadParser != null) { + if (level_ >= Level.PayloadLevel.value) { + currentPayloadParser.handleEndElement(element, ns); + } + if (level_ == Level.PayloadLevel.value) { + getPayloadInternal().setPayload(currentPayloadParser.getPayload()); + currentPayloadParser= null; + } + } + else if (level_ == Level.PayloadLevel.value) { + if (element.equals("text")) { + payloadInternal.setText(currentText_); + } + } + } + + @Override + public void handleCharacterData(final String data) { + if (level_ > Level.PayloadLevel.value && currentPayloadParser != null) { + currentPayloadParser.handleCharacterData(data); + }else{ + currentText_ += data; + } + } +} diff --git a/src/com/isode/stroke/parser/payloadparsers/ErrorParserFactory.java b/src/com/isode/stroke/parser/payloadparsers/ErrorParserFactory.java new file mode 100644 index 0000000..1f36b19 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/ErrorParserFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2011 Kevin Smith + * All rights reserved. + */ +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PayloadParserFactory; + +/** + * Parser factory for {@link ErrorPayload} + * + */ +public class ErrorParserFactory implements PayloadParserFactory { + private FullPayloadParserFactoryCollection factories_; + + /** + * Create the factory + * @param factories reference to Payload parser factory collection, not null + */ + public ErrorParserFactory(FullPayloadParserFactoryCollection factories) { + this.factories_ = factories; + } + + @Override + public boolean canParse(final String element, final String ns, final AttributeMap map) { + return element.equals("error"); + } + + @Override + public PayloadParser createPayloadParser() { + return new ErrorParser(factories_); + } + + @Override + public String toString() { + return ErrorParserFactory.class.getSimpleName(); + } +} diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index 7273cf2..6b67116 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -23,7 +23,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory("body", BodyParser.class)); //addFactory(new GenericPayloadParserFactory("subject", SubjectParser.class)); //addFactory(new GenericPayloadParserFactory("priority", PriorityParser.class)); - //addFactory(new ErrorParserFactory(this))); + addFactory(new ErrorParserFactory(this)); addFactory(new SoftwareVersionParserFactory()); //addFactory(new StorageParserFactory()); addFactory(new RosterParserFactory()); -- cgit v0.10.2-6-g49f6