WSUtils.java 6.08 KB
Newer Older
1 2 3 4
/**
 * $Revision$
 * $Date$
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * 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.
18 19 20 21 22
 */
package org.jivesoftware.openfire.clearspace;

import org.dom4j.Element;
import org.dom4j.Node;
23
import org.xmpp.packet.JID;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Several utilities to handle REST webservices.
 */
public class WSUtils {

    /*
     * Date formats to parse and format REST dates. There are two types, with and without milliseconds
     */
    private static final SimpleDateFormat dateFormatMil = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    private static final SimpleDateFormat dateFormatNoMil = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    /**
     * Returns the text of the first an element with name 'return'.
44
     *
45 46 47 48 49 50 51 52 53
     * @param element the element to search for a return element.
     * @return the text of the return element.
     */
    protected static String getReturn(Element element) {
        return getElementText(element, "return");
    }

    /**
     * Returns the text of the first an element with name 'name'.
54
     *
55 56 57 58 59 60 61 62 63 64 65 66 67 68
     * @param node the element to search for a "name" element.
     * @param name the name of the element to search
     * @return the text of the corresponding element
     */
    protected static String getElementText(Node node, String name) {
        Node n = node.selectSingleNode(name);
        if (n != null) {
            return n.getText();
        }
        return null;
    }

    /**
     * Modifies the text of the elmement with name 'name'.
69 70 71
     *
     * @param node     the element to search
     * @param name     the name to search
72 73 74 75 76 77 78
     * @param newValue the new value of the text
     */
    protected static void modifyElementText(Node node, String name, String newValue) {
        Node n = node.selectSingleNode(name);
        n.setText(newValue);
    }

79 80 81
    protected static void modifyElementText(Element element, String[] path, String newValue) {
        Element e = element;
        for (String s : path) {
82 83 84 85 86 87
            Element subElement = e.element(s);
            if (subElement == null) {
                // Add the element if associated string was not in path.
                subElement = e.addElement(s);
            }
            e = subElement;
88 89 90 91 92
        }
        e.setText(newValue);
    }


93 94
    /**
     * Parse REST responses of the type String[], that are XML of the form:
95
     * <p/>
96
     * <something>
97 98 99
     * <return>text1</return>
     * <return>text2</return>
     * <return>text3</return>
100
     * </something>
101
     *
102 103
     * @param element Element from REST response to be parsed.
     * @return An array of strings from the REST response.
104 105 106
     */
    protected static List<String> parseStringArray(Element element) {
        List<String> list = new ArrayList<String>();
107
        @SuppressWarnings("unchecked")
108 109 110 111 112 113 114
        List<Node> nodes = (List<Node>) element.selectNodes("return");
        for (Node node : nodes) {
            list.add(node.getText());
        }
        return list;
    }

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    /**
     * Parse REST responses of the type String[] that represent usernames, that are XML of the form:
     * <p/>
     * <something>
     * <return>text1</return>
     * <return>text2</return>
     * <return>text3</return>
     * </something>
     *
     * @param element Element from REST response to be parsed.
     * @return An array of strings from the REST response.
     */
    protected static List<String> parseUsernameArray(Element element) {
        List<String> list = new ArrayList<String>();
        @SuppressWarnings("unchecked")
        List<Node> nodes = (List<Node>) element.selectNodes("return");
        for (Node node : nodes) {
            String username = node.getText();
            // Escape username.
            username = JID.escapeNode(username);
            list.add(username);
        }
        return list;
    }

140 141 142 143 144 145 146
    protected static String marshallList(List<String> data) {
        String result = "";

        for (String s : data) {
            result += s + ",";
        }

147
        return result.substring(0, result.length() - 1);
148 149 150 151 152
    }

    /**
     * Parses a date of the form 1969-12-31T21:00:00-03:00, or 2008-02-13T18:54:29.147-03:00.
     * If the string is null or there is a problem parsing the date, returns null.
153
     *
154 155 156 157 158 159 160 161 162 163
     * @param date the string to parse
     * @return the corresponding date, or null if t
     */
    public static Date parseDate(String date) {
        if (date == null) {
            return null;
        }
        // REST writes dates time zone with ':', somthing like -3:00
        // to parse it they should be removed
        int index = date.lastIndexOf(":");
164
        date = date.substring(0, index) + date.substring(index + 1);
165 166 167 168
        Date d = null;
        try {
            if (date.length() == 24) {
                d = dateFormatNoMil.parse(date);
169
            } else {
170 171 172 173 174 175 176 177 178 179
                d = dateFormatMil.parse(date);
            }
        } catch (ParseException e) {
            // can't parse it, return null
        }
        return d;
    }

    /**
     * Formats a date into yyyy-MM-dd'T'HH:mm:ss.SSSZ, for example 2008-02-13T18:54:29.147-03:00
180
     *
181 182 183 184 185 186 187 188 189 190 191
     * @param date the date to format
     * @return a string representation of the date
     */
    public static String formatDate(Date date) {
        // REST writes dates time zone with ':', somthing like -3:00
        // to format it they should be added
        String d = dateFormatMil.format(date);
        d = d.substring(0, d.length() - 2) + ":" + d.substring(d.length() - 2);
        return d;
    }
}