Back to project page android-http.
The source code is released under:
Apache License
If you think the Android project android-http listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2012, 2013 the diamond:dogs|group *//from ww w.ja v a 2 s . c o m * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package at.diamonddogs.service.processor; import java.util.Vector; import org.ksoap2.SoapFault; import org.ksoap2.serialization.AttributeContainer; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import android.R.string; import android.content.Context; import android.os.Handler; import android.os.Message; import at.diamonddogs.data.adapter.ReplyAdapter; import at.diamonddogs.data.adapter.ReplyAdapter.Status; import at.diamonddogs.data.adapter.soap.SoapByteArrayAdapter; import at.diamonddogs.data.adapter.soap.SoapReplyAdapter; import at.diamonddogs.data.dataobjects.Request; import at.diamonddogs.data.dataobjects.SoapReply; import at.diamonddogs.data.dataobjects.WebReply; import at.diamonddogs.data.dataobjects.WebRequest; import at.diamonddogs.exception.ProcessorExeception; import at.diamonddogs.util.CacheManager.CachedObject; /** * Abstract base class for SOAP requests * * @param <T> * the output object */ public abstract class SoapProcessor<T> extends ServiceProcessor<T> implements SynchronousProcessor<T> { private static final Logger LOGGER = LoggerFactory.getLogger(SoapProcessor.class.getSimpleName()); /** * {@inheritDoc} */ @Override public T obtainDataObjectFromCachedObject(Context c, WebRequest webRequest, CachedObject object) { try { return obtainDataObjectFromResult(c, getResult((byte[]) object.getCachedObject()), null); } catch (Throwable tr) { LOGGER.error("Could not obtain data object ", tr); return null; } } /** * {@inheritDoc} */ @Override public void processCachedObject(CachedObject cachedObject, Handler handler, Request request) { try { dispatchResultToHandler(null, handler, null, getResult((byte[]) cachedObject.getCachedObject())); } catch (Throwable tr) { handler.sendMessage(createErrorMessage(tr, (WebRequest) request)); } } /** * This method should not be overridden, will pre-process a SOAP reply and * coordinate callbacks and direct processing output to the appropriate * {@link Handler} */ @Override public void processWebReply(Context c, ReplyAdapter r, Handler handler) { if (r.getStatus() == Status.OK) { Object result; try { result = getResult(r); } catch (Throwable tr) { LOGGER.warn("Problem while getting soap result.", tr); handler.sendMessage(createErrorMessage(tr, r)); return; } LOGGER.debug("processing SoapReply"); dispatchResultToHandler(c, handler, r, result); } else { handler.sendMessage(createErrorMessage(r)); } } /** * A dispatcher method that takes * * @param c * a {@link Context}, may be <code>null</code> (i.e. if the * method is called to create a reply from a cached object) * @param handler * the {@link Handler} that will receive the result of the * operation * @param r * a {@link ReplyAdapter}, may be <code>null</code> (i.e. if the * method is called to create a reply from a cached object) * @param result * the result generated by * {@link SoapSerializationEnvelope#getResponse()} */ private void dispatchResultToHandler(Context c, Handler handler, ReplyAdapter r, Object result) { try { if (result == null) { LOGGER.debug("processSoapNull"); handler.sendMessage(processSoapNull(r)); } else if (result instanceof SoapFault) { LOGGER.debug("processSoapFault"); handler.sendMessage(processSoapFault(r, (SoapFault) result)); } else if (result instanceof SoapObject) { LOGGER.debug("processSoapObject"); handler.sendMessage(createReturnMessage(r, processSoapReply(c, r, (SoapObject) result))); } else if (result instanceof SoapPrimitive) { LOGGER.debug("processSoapPrimitive"); handler.sendMessage(createReturnMessage(r, processSoapReply(c, r, (SoapPrimitive) result))); } else if (result instanceof Vector<?>) { handler.sendMessage(createReturnMessage(r, processSoapReply(c, r, (Vector<?>) result))); } else { handler.sendMessage(createErrorMessage(r)); } } catch (Exception e2) { LOGGER.debug("processSoap - failed", e2); handler.sendMessage(createErrorMessage(e2, r)); } } private Object getResult(ReplyAdapter r) throws Throwable { SoapReply soapReply = new SoapReplyAdapter((WebReply) r.getReply()).getReply(); SoapSerializationEnvelope e = soapReply.getEnvelope(); return e.getResponse(); } private Object getResult(byte[] data) throws Throwable { return new SoapByteArrayAdapter(data).getEnvelope().getResponse(); } /** * * {@inheritDoc} */ @Override public T obtainDataObjectFromWebReply(Context c, ReplyAdapter replyAdapter) { Object result; try { result = getResult(replyAdapter); } catch (Throwable tr) { LOGGER.error("Error while obtaining result.", tr); throw new ProcessorExeception(tr); } return obtainDataObjectFromResult(c, result, replyAdapter); } /** * Obtains the data {@link Object} by calling appropriate callback methods. * * @param c * a {@link Context} * @param result * the result generated by * {@link SoapSerializationEnvelope#getResponse()} * @param replyAdapter * the {@link ReplyAdapter} containing the reply information that * resulted in result. * @return a data {@link Object} of type T */ private T obtainDataObjectFromResult(Context c, Object result, ReplyAdapter replyAdapter) { if (result == null) { return null; } else if (result instanceof SoapFault) { throw new ProcessorExeception((SoapFault) result); } else if (result instanceof SoapObject) { return processSoapReply(c, replyAdapter, (SoapObject) result); } else if (result instanceof SoapPrimitive) { return processSoapReply(c, replyAdapter, (SoapPrimitive) result); } else if (result instanceof Vector<?>) { return processSoapReply(c, replyAdapter, (Vector<?>) result); } else { throw new ProcessorExeception("Unknown error"); } } /** * Called when the SOAP result is null * * @param replyAdapter * the {@link ReplyAdapter} * @return a message Object */ protected Message processSoapNull(ReplyAdapter replyAdapter) { return createErrorMessage(replyAdapter); } /** * Called when a SOAP result should be processed * * @param c * a {@link Context}, may be <code>null</code> (i.e. if the * method is called to create a reply from a cached object) * @param replyAdapter * a {@link ReplyAdapter}, may be <code>null</code> when the data * to be processed is read from the cache. * @param o * the result vector * @return */ protected T processSoapReply(Context c, ReplyAdapter replyAdapter, Vector<?> o) { throw new UnsupportedOperationException("Vector processing not implemented"); } /** * Called when a SOAP result should be processed * * @param c * a {@link Context}, may be <code>null</code> (i.e. if the * method is called to create a reply from a cached object) * @param replyAdapter * a {@link ReplyAdapter}, may be <code>null</code> when the data * to be processed is read from the cache. * @param o * the {@link SoapObject} created by * {@link SoapProcessor#processWebReply(Context, ReplyAdapter, Handler)} * @return an output object of type T */ protected abstract T processSoapReply(Context c, ReplyAdapter replyAdapter, SoapObject o); /** * Called when a SOAP result should be processed * * @param c * a {@link Context}, may be <code>null</code> (i.e. if the * method is called to create a reply from a cached object) * @param replyAdapter * a {@link ReplyAdapter}, may be <code>null</code> when the data * to be processed is read from the cache. * @param o * the {@link SoapPrimitive} created by * {@link SoapProcessor#processWebReply(Context, ReplyAdapter, Handler)} * @return an output object of type T */ protected abstract T processSoapReply(Context c, ReplyAdapter replyAdapter, SoapPrimitive o); /** * Called if the result is a {@link SoapFault} * * @param replyAdapter * a {@link ReplyAdapter}, may be <code>null</code> when the data * to be processed is read from the cache. * @param fault * the {@link SoapFault} * @return a {@link Message} */ protected Message processSoapFault(ReplyAdapter replyAdapter, SoapFault fault) { return createErrorMessage(fault, replyAdapter); } /** * Gets a boolean from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>false</code> if the boolean could not be parsed or if the * boolean was <code>false</code>, <code>true</code> otherwise */ protected boolean getBoolean(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getBoolean(s); } /** * Gets a boolean from a {@link SoapPrimitive} using the property identified * by name * * @param o * the {@link SoapPrimitive} * * @return <code>false</code> if the boolean could not be parsed or if the * boolean was <code>false</code>, <code>true</code> otherwise */ protected boolean getBoolean(SoapPrimitive o) { String s = getStringFromSoapPrimitive(o); return getBoolean(s); } /** * Gets a boolean from {@link AttributeContainer} attribute using the * attributes name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * @param name * the name of the attribute * @return <code>false</code> if the boolean could not be parsed or if the * boolean was <code>false</code>, <code>true</code> otherwise */ protected boolean getBooleanFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getBoolean(s); } /** * Gets a byte from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>0</code> if the byte could not be parsed or if the byte was * <code>0</code>, the value otherwise */ protected byte getByte(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getByte(s); } /** * Gets a byte from a {@link SoapPrimitive} using the property identified by * name * * @param o * the {@link SoapPrimitive} * @return <code>0</code> if the byte could not be parsed or if the byte was * <code>0</code>, the value otherwise */ protected byte getByte(SoapPrimitive o) { String s = getStringFromSoapPrimitive(o); return getByte(s); } /** * Gets a byte from {@link AttributeContainer} attribute using the * attributes name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * @param name * the name of the attribute * @return <code>0</code> if the byte could not be parsed or if the byte was * <code>0</code>, the value otherwise */ protected byte getByteFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getByte(s); } /** * Gets a short from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>0</code> if the short could not be parsed or if the short * was <code>0</code>, the value otherwise */ protected short getShort(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getShort(s); } /** * Gets a short from a {@link SoapPrimitive} using the property identified * by * name * * @param o * the {@link SoapPrimitive} * @return <code>0</code> if the short could not be parsed or if the short * was <code>0</code>, the value otherwise */ protected short getShort(SoapPrimitive o, String name) { String s = getStringFromSoapPrimitive(o); return getShort(s); } /** * Gets a short from {@link AttributeContainer} attribute using the * attributes name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * @param name * the name of the attribute * @return <code>0</code> if the short could not be parsed or if the short * was <code>0</code>, the value otherwise */ protected short getShortFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getShort(s); } /** * Gets a char from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>\0</code> if the char could not be parsed or if the char * was <code>\0</code>, the value otherwise */ protected char getChar(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getChar(s); } /** * Gets a char from a {@link SoapPrimitive} using the property identified by * name * * @param o * the {@link SoapPrimitive} * @return <code>\0</code> if the char could not be parsed or if the char * was <code>\0</code>, the value otherwise */ protected char getChar(SoapPrimitive o) { String s = getStringFromSoapPrimitive(o); return getChar(s); } /** * Gets a char from {@link AttributeContainer} attribute using the * attributes name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * @param name * the name of the attribute * @return <code>\0</code> if the char could not be parsed or if the char * was <code>\0</code>, the value otherwise */ protected char getCharFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getChar(s); } /** * Gets a int from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>0</code> if the int could not be parsed or if the int was * <code>0</code>, the value otherwise */ protected int getInt(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getInt(s); } /** * Gets a int from a {@link SoapPrimitive} using the property identified by * name * * @param o * the {@link SoapPrimitive} * @return <code>0</code> if the int could not be parsed or if the int was * <code>0</code>, the value otherwise */ protected int getInt(SoapPrimitive o, String name) { String s = getStringFromSoapPrimitive(o); return getInt(s); } /** * Gets a int from {@link AttributeContainer} attribute using the attributes * name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * @param name * the name of the attribute * @return <code>0</code> if the int could not be parsed or if the int was * <code>0</code>, the value otherwise */ protected int getIntFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getInt(s); } /** * Gets a long from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>0</code> if the long could not be parsed or if the long was * <code>0</code>, the value otherwise */ protected long getLong(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getLong(s); } /** * Gets a long from a {@link SoapPrimitive} using the property identified by * name * * @param o * the {@link SoapPrimitive} * * @return <code>0</code> if the long could not be parsed or if the long was * <code>0</code>, the value otherwise */ protected long getLong(SoapPrimitive o) { String s = getStringFromSoapPrimitive(o); return getLong(s); } /** * Gets a long from {@link AttributeContainer} attribute using the * attributes * name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * @param name * the name of the attribute * @return <code>0</code> if the long could not be parsed or if the long was * <code>0</code>, the value otherwise */ protected long getLongFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getLong(s); } /** * Gets a float from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>0.0f</code> if the float could not be parsed or if the * float was <code>0.0f</code>, the value otherwise */ protected float getFloat(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getFloat(s); } /** * Gets a float from a {@link SoapPrimitive} using the property identified * by name * * @param o * the {@link SoapPrimitive} * @return <code>0.0f</code> if the float could not be parsed or if the * float was <code>0.0f</code>, the value otherwise */ protected float getFloat(SoapPrimitive o) { String s = getStringFromSoapPrimitive(o); return getFloat(s); } /** * Gets a long from {@link AttributeContainer} attribute using the * attributes * name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * * @param name * the name of the attribute * * @return <code>0.0f</code> if the float could not be parsed or if the * float was <code>0.0f</code>, the value otherwise */ protected float getFloatFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getFloat(s); } /** * Gets a double from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @return <code>0.0d</code> if the double could not be parsed or if the * double was <code>0.0d</code>, the value otherwise */ protected double getDouble(SoapObject o, String name) { String s = getStringFromSoapObject(o, name); return getDouble(s); } /** * Gets a double from a {@link SoapPrimitive} using the property identified * by name * * @param o * the {@link SoapPrimitive} * @return <code>0.0d</code> if the double could not be parsed or if the * double was <code>0.0d</code>, the value otherwise */ protected double getDouble(SoapPrimitive o) { String s = getStringFromSoapPrimitive(o); return getDouble(s); } /** * Gets a long from {@link AttributeContainer} attribute using the * attributes * name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * * @param name * the name of the attribute * * @return <code>0.0d</code> if the double could not be parsed or if the * double was <code>0.0d</code>, the value otherwise */ protected double getDoubleFromAttribute(AttributeContainer c, String name) { String s = getStringFromAttributeContainer(c, name); return getDouble(s); } /** * Gets an enum from a {@link SoapObject} using the property identified by * name * * @param o * the {@link SoapObject} * @param name * the name of a property * @param cls * the {@link Class} {@link Object} of the {@link Enum} * @return the {@link Enum} or <code>null</code> if the enum could not be * parsed */ @SuppressWarnings("hiding") protected <T extends Enum<T>> T getEnum(SoapObject o, String name, Class<T> cls) { String s = getStringFromSoapObject(o, name); return getEnum(s, cls); } /** * * Gets an {@link Enum} from a {@link SoapPrimitive} using the property * identified * by name * * @param <T> * @param o * the {@link SoapPrimitive} * @param name * the name of a property * @param cls * the {@link Class} {@link Object} of the {@link Enum} * @return the {@link Enum} or <code>null</code> if the enum could not be * parsed */ @SuppressWarnings("hiding") protected <T extends Enum<T>> T getEnum(SoapPrimitive o, String name, Class<T> cls) { String s = getStringFromSoapPrimitive(o); return getEnum(s, cls); } /** * Gets an enum from {@link AttributeContainer} attribute using the * attributes * name * identified by name * * @param c * the {@link AttributeContainer} whose attribute to get * @param name * the name of a attribute * @param cls * the {@link Class} {@link Object} of the {@link Enum} * @return the {@link Enum} or <code>null</code> if the enum could not be * parsed */ @SuppressWarnings("hiding") protected <T extends Enum<T>> T getEnumFromAttribute(AttributeContainer c, String name, Class<T> cls) { String s = getStringFromAttributeContainer(c, name); return getEnum(s, cls); } /** * Gets the {@link String} from a {@link SoapObject} using the property * identified by name * * @param o * the {@link SoapPrimitive} * @param name * the name of a property * @return a {@link String} */ protected String getStringFromSoapObject(SoapObject o, String name) { if (!o.hasProperty(name)) { return ""; } String s = o.getPropertyAsString(name); if (isStringEmpty(s) || s.equals("anyType{}")) { return ""; } else { return s; } } /** * Gets the {@link String} from a {@link SoapPrimitive} using the property * identified by name * * @param o * the {@link SoapPrimitive} * @return a {@link String} */ protected String getStringFromSoapPrimitive(SoapPrimitive o) { String s = o.toString(); if (isStringEmpty(s) || s.equals("anyType{}")) { return ""; } else { return s; } } /** * Gets the {@link String} value of the attribute identified by attribute of * the {@link AttributeContainer} c * * @param c * the container that holds the attribute * @param attribute * the name of the attribute * @return the attribute as a {@link string} */ protected String getStringFromAttributeContainer(AttributeContainer c, String attribute) { if (!c.hasAttribute(attribute)) { return ""; } return (String) c.getAttributeSafelyAsString(attribute); } }