1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.web.jcr.rest.client.json;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.UnsupportedEncodingException;
29 import java.net.HttpURLConnection;
30 import java.net.URLDecoder;
31 import java.net.URLEncoder;
32 import org.modeshape.common.util.CheckArg;
33
34 /**
35 * The <code>JsonUtils</code> class provides utilities needed to work with the ModeShape REST server JSON API.
36 */
37 public final class JsonUtils {
38
39 // ===========================================================================================================================
40 // Class Methods
41 // ===========================================================================================================================
42
43 /**
44 * The default character set being used.
45 */
46 private static final String DEFAULT_CHARSET = "UTF-8"; // TODO need to property drive charset
47
48 // ===========================================================================================================================
49 // Class Methods
50 // ===========================================================================================================================
51
52 /**
53 * @param text the text being URL decoded (never <code>null</code>)
54 * @return the decoded text
55 * @throws UnsupportedEncodingException if the charset is not supported
56 */
57 public static String decode( String text ) throws UnsupportedEncodingException {
58 CheckArg.isNotNull(text, "text");
59 return URLDecoder.decode(text, DEFAULT_CHARSET);
60 }
61
62 /**
63 * Forward slashes ('/') are not encoded.
64 *
65 * @param text the text being URL encoded (never <code>null</code>)
66 * @return the decoded text
67 * @throws UnsupportedEncodingException if the charset is not supported
68 */
69 public static String encode( String text ) throws UnsupportedEncodingException {
70 CheckArg.isNotNull(text, "text");
71
72 // don't encode '/' as it needs to stay that way in the URL
73 StringBuilder encoded = new StringBuilder();
74
75 for (char c : text.toCharArray()) {
76 encoded.append((c == '/') ? c : URLEncoder.encode(Character.toString(c), DEFAULT_CHARSET));
77 }
78
79 return encoded.toString();
80 }
81
82 /**
83 * Note: The connection is not disconnected during this method.
84 *
85 * @param connection the connection whose input stream is going to be read from (never <code>null</code>)
86 * @return the data read from the connection input stream (never <code>null</code>)
87 * @throws IOException if there is a problem reading from the connection
88 */
89 public static String readInputStream( HttpURLConnection connection ) throws IOException {
90 CheckArg.isNotNull(connection, "connection");
91
92 InputStream stream = connection.getInputStream();
93 int bytesRead;
94 byte[] bytes = new byte[1024];
95 StringBuffer buff = new StringBuffer();
96
97 while (-1 != (bytesRead = stream.read(bytes, 0, 1024))) {
98 buff.append(new String(bytes, 0, bytesRead));
99 }
100
101 return buff.toString();
102 }
103
104 // ===========================================================================================================================
105 // Constructors
106 // ===========================================================================================================================
107
108 /**
109 * Don't allow construction.
110 */
111 private JsonUtils() {
112 // nothing to do
113 }
114
115 }