Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Matrix.java
1 
16 package net.squiz.matrix.core;
17 
18 import java.net.*;
19 import javax.xml.parsers.DocumentBuilder;
20 import org.w3c.dom.*;
21 import org.xml.sax.SAXException;
22 import java.io.*;
23 import java.security.*;
24 import java.util.*;
25 import java.text.MessageFormat;
26 
43 public class Matrix {
44 
45  private static Properties properties = new Properties();
46  private static Properties translations = new Properties();
47 
48  // cannot instantiate
49  private Matrix() {}
50 
55  public static final String translate(String key) {
56  if (!translations.containsKey(key))
57  System.out.println("Translation " + key + " not found");
58  return translations.getProperty(key, key);
59  }
60 
66  public static final String translate(String key, Object[] arguments) {
67  if (!translations.containsKey(key))
68  System.out.println("Translation " + key + " not found");
69  String rawTranslation = translations.getProperty(key, key);
70  return MessageFormat.format(rawTranslation, arguments);
71  }
72 
78  public static final void setTranslationFile(String propertiesFile) {
79  try {
80  translations.load(new ByteArrayInputStream(propertiesFile.getBytes()));
81  } catch (IOException ioe) {
82  ioe.printStackTrace();
83  }
84  }
85 
90  public static final String getProperty(String key) {
91  return properties.getProperty(key);
92  }
93 
100  public static final String getProperty(String key, String defaultValue) {
101  return properties.getProperty(key, defaultValue);
102  }
103 
109  public static final void setProperty(String key, String value) {
110  properties.setProperty(key, value);
111  }
112 
120  public static Document doRequest(String xml) throws IOException {
121 
122  final String myxml = xml;
123  // Use provileged mode when making request to asset_map.inc because jsToJavaCall() fails on default security policy
124  URLConnection conn = AccessController.doPrivileged(
125  new PrivilegedAction<URLConnection>() {
126  public URLConnection run() {
127  URL execURL = null;
128  URLConnection conn = null;
129  try {
130  String basePath = getProperty("parameter.url.baseurl");
131  String execPath = basePath + getProperty("parameter.backendsuffix")
132  + "/?SQ_ACTION=asset_map_request&SESSION_ID="
133  + getProperty("parameter.sessionid") + "&SESSION_KEY=" + getProperty("parameter.sessionkey");
134  execURL = new URL(execPath);
135  } catch (MalformedURLException mue) {
136  mue.printStackTrace();
137  }
138 
139  try {
140  conn = execURL.openConnection();
141  conn.setUseCaches(false);
142  conn.setDoOutput(true);
143 
144  ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
145  PrintWriter out = new PrintWriter(byteStream, true);
146  out.print(myxml);
147  out.flush();
148 
149  conn.setRequestProperty("Content-Length", String.valueOf(byteStream.size()));
150  conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
151  conn.setRequestProperty("Cache-Control", "no-store, no-cache, " +
152  "must-revalidate, post-check=0, pre-check=0");
153 
154  byteStream.writeTo(conn.getOutputStream());
155 
156  } catch (IOException ioe) {
157  //throw new IOException("error while getting request connection : " + ioe.getMessage());
158  ioe.printStackTrace();
159  }
160  return conn;
161 
162  }}
163  );
164 
165  Document document = null;
166  DocumentBuilder builder = null;
167 
168  try {
169  builder = XML.getParser();
170  } catch (SAXException se) {
171  throw new IOException("Could not do the mysource request: " + se.getMessage());
172  }
173 
174  try {
175  document = builder.parse(conn.getInputStream());
176  } catch (SAXException se) {
177  // we will get into here if no XML is returned from
178  // conn (the DB daemon hasn't started?)
179  throw new IOException("error while parsing : " + se.getMessage());
180  }
181 
182  if (document.getDocumentElement().getTagName().equals("error")) {
183  Element errorElement = (Element) document.getDocumentElement();
184  throw new IOException("error while getting response: "
185  + errorElement.getFirstChild().getNodeValue());
186  }
187  return document;
188  }
189 }