001package jmri.jmrit.logixng.actions.configurexml;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.logixng.*;
008import jmri.jmrit.logixng.actions.WebRequest;
009import jmri.jmrit.logixng.actions.WebRequest.RequestMethodType;
010import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectStringXml;
012
013import org.jdom2.Element;
014
015/**
016 * Handle XML configuration for WebRequest objects.
017 *
018 * @author Bob Jacobsen      Copyright: Copyright (c) 2004, 2008, 2010
019 * @author Daniel Bergqvist  Copyright (C) 2023
020 */
021public class WebRequestXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public WebRequestXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a WebRequest
028     *
029     * @param o Object to store, of type WebRequest
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        WebRequest p = (WebRequest) o;
035
036        Element element = new Element("WebRequest");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, element);
041
042        Element e2 = new Element("Socket");
043        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
044        MaleSocket socket = p.getSocket().getConnectedSocket();
045        String socketSystemName;
046        if (socket != null) {
047            socketSystemName = socket.getSystemName();
048        } else {
049            socketSystemName = p.getSocketSystemName();
050        }
051        if (socketSystemName != null) {
052            e2.addContent(new Element("systemName").addContent(socketSystemName));
053        }
054        element.addContent(e2);
055
056        var selectRequestMethodXml = new LogixNG_SelectEnumXml<RequestMethodType>();
057        var selectUrlXml = new LogixNG_SelectStringXml();
058        var selectUserAgentXml = new LogixNG_SelectStringXml();
059
060        element.addContent(selectRequestMethodXml.store(p.getSelectRequestMethod(), "requestMethod"));
061        element.addContent(selectUrlXml.store(p.getSelectUrl(), "url"));
062        element.addContent(selectUserAgentXml.store(p.getSelectUserAgent(), "userAgent"));
063
064        element.addContent(new Element("localVariableForResponseCode").addContent(p.getLocalVariableForResponseCode()));
065        element.addContent(new Element("localVariableForReplyContent").addContent(p.getLocalVariableForReplyContent()));
066        element.addContent(new Element("localVariableForCookies").addContent(p.getLocalVariableForCookies()));
067
068        Element parameters = new Element("Parameters");
069        for (WebRequest.Parameter parameter : p.getParameters()) {
070            Element elementParameter = new Element("Parameter");
071            elementParameter.addContent(new Element("name").addContent(parameter.getName()));
072            elementParameter.addContent(new Element("type").addContent(parameter.getType().name()));
073            elementParameter.addContent(new Element("data").addContent(parameter.getData()));
074            parameters.addContent(elementParameter);
075        }
076        element.addContent(parameters);
077
078        return element;
079    }
080
081    @Override
082    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
083
084        Element socketNameElement = shared.getChild("Socket").getChild("socketName");
085        String socketName = socketNameElement.getTextTrim();
086        Element socketSystemNameElement = shared.getChild("Socket").getChild("systemName");
087        String socketSystemName = null;
088        if (socketSystemNameElement != null) {
089            socketSystemName = socketSystemNameElement.getTextTrim();
090        }
091
092        String sys = getSystemName(shared);
093        String uname = getUserName(shared);
094        WebRequest h = new WebRequest(sys, uname);
095
096        loadCommon(h, shared);
097
098        h.getChild(0).setName(socketName);
099        h.setSocketSystemName(socketSystemName);
100
101        var selectRequestMethodXml = new LogixNG_SelectEnumXml<RequestMethodType>();
102        var selectUrlXml = new LogixNG_SelectStringXml();
103        var selectUserAgentXml = new LogixNG_SelectStringXml();
104
105        selectRequestMethodXml.load(shared.getChild("requestMethod"), h.getSelectRequestMethod());
106        selectUrlXml.load(shared.getChild("url"), h.getSelectUrl());
107        selectUserAgentXml.load(shared.getChild("userAgent"), h.getSelectUserAgent());
108
109        Element elem = shared.getChild("localVariableForResponseCode");
110        if (elem != null) h.setLocalVariableForResponseCode(elem.getTextTrim());
111
112        elem = shared.getChild("localVariableForReplyContent");
113        if (elem != null) h.setLocalVariableForReplyContent(elem.getTextTrim());
114
115        elem = shared.getChild("localVariableForCookies");
116        if (elem != null) h.setLocalVariableForCookies(elem.getTextTrim());
117
118        List<Element> parameters = shared.getChild("Parameters").getChildren();  // NOI18N
119        log.debug("Found {} dataList", parameters.size() );  // NOI18N
120
121        for (Element e : parameters) {
122            Element elementName = e.getChild("name");
123            if (elementName == null) throw new IllegalArgumentException("Element 'name' does not exists");
124
125            SymbolTable.InitialValueType type = SymbolTable.InitialValueType.LocalVariable;
126            Element elementType = e.getChild("type");
127            if (elementType != null) {
128                type = SymbolTable.InitialValueType.valueOf(elementType.getTextTrim());
129            }
130
131            Element elementData = e.getChild("data");
132            if (elementData == null) throw new IllegalArgumentException("Element 'data' does not exists");
133
134            h.getParameters().add(new WebRequest.Parameter(elementName.getTextTrim(), type, elementData.getTextTrim()));
135        }
136
137        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
138        return true;
139    }
140
141    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WebRequestXml.class);
142}