source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/Helpers.java @ 5266

Last change on this file since 5266 was 5266, checked in by olhsha@mpi.nl, 10 years ago

Fixing automatic generation of a shibbolized user record and fixing update principals (both bugs appeared after refactoring). Fixing dynamic logout link. Fixing jsp page.

File size: 4.5 KB
Line 
1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend;
19
20import eu.dasish.annotation.schema.Principal;
21import java.io.ByteArrayInputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.util.Map;
25import javax.xml.parsers.DocumentBuilder;
26import javax.xml.parsers.DocumentBuilderFactory;
27import javax.xml.parsers.ParserConfigurationException;
28import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.w3c.dom.ls.DOMImplementationLS;
32import org.w3c.dom.ls.LSSerializer;
33import org.xml.sax.SAXException;
34
35/**
36 *
37 * @author olhsha
38 */
39public class Helpers {
40
41    //exception messages
42    final static public String INVALID_BODY_EXCEPTION = "Invalide annotation body: both, text and xml options, are null.";
43
44    public static String replace(String text, Map<String, ?> pairs) {
45        StringBuilder result = new StringBuilder(text);
46        for (String old : pairs.keySet()) {
47            if (old != null) {
48                if (!old.equals("")) {
49                    replaceString(result, old, pairs.get(old));
50                }
51            }
52        }
53        return result.toString();
54    }
55
56    public static StringBuilder replaceString(StringBuilder source, String oldFragment, Object newObject) {
57        if (oldFragment != null) {
58            int lengthOld = oldFragment.length();
59            String newFragment;
60            if (newObject != null) {
61                if (newObject instanceof Integer) {
62                    newFragment = ((Integer) newObject).toString();
63                } else {
64                    if (newObject instanceof String) {
65                        newFragment = (String) newObject;
66                    } else {
67                        newFragment = newObject.toString();
68                    }
69                }
70            } else {
71                newFragment = " ";
72            }
73            int lengthNew = newFragment.length();
74            int indexOf = source.indexOf(oldFragment);
75            while (indexOf > 0) {
76                source.delete(indexOf, indexOf + lengthOld);
77                source.insert(indexOf, newFragment);
78                indexOf = source.indexOf(oldFragment, indexOf + lengthNew);
79            }
80        }
81        return source;
82    }
83
84    public static Element stringToElement(String string) throws ParserConfigurationException, IOException, SAXException {
85        DocumentBuilder dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder();
86        InputStream is = new ByteArrayInputStream(string.getBytes("UTF-16"));
87        Document doc = dbf.parse(is);
88        return doc.getDocumentElement();
89
90    }
91
92    public static String elementToString(Element element) {
93        Document document = element.getOwnerDocument();
94        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
95        LSSerializer serializer = domImplLS.createLSSerializer();
96        String result = serializer.writeToString(element);
97        return result;
98    }
99
100    public static String welcomeString(String baseUri, String remoteID) {
101        String welcome = "<!DOCTYPE html><body>"
102                + "You are logged in as "
103                + remoteID + ".<br>"
104                + "<h3>Welcome to DASISH Webannotator (DWAN)</h3><br>"
105                + "<a href=\"" + baseUri + "\"> To DWAN's test jsp page</a>"
106                + "</body>";
107        return welcome;
108    }
109
110    public static Principal createPrincipalElement(String name, String e_mail) {
111        Principal result = new Principal();
112        result.setDisplayName(name);
113        result.setEMail(e_mail);
114        return result;
115    }
116
117    public static String hashPswd(String pswd, int strength, String salt) {
118        ShaPasswordEncoder encoder = new ShaPasswordEncoder(strength);
119        return encoder.encodePassword(pswd, salt);
120    }
121}
Note: See TracBrowser for help on using the repository browser.