Debian Codegraph @

src/java/org/astrogrid/samp/bridge/UrlExporter.java

raw
1 package org.astrogrid.samp.bridge;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.ListIterator;
10 import java.util.Map;
11 import java.util.logging.Logger;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14 import org.astrogrid.samp.httpd.UtilServer;
15
16 /**
17 * Exports SAMP data objects which have been created on a given host
18 * for use in a remote context. The job that needs doing is to
19 * convert URLs which reference the host in a way that only makes sense
20 * locally (as a loopback address 127.0.0.1 or localhost) to a form
21 * which can be used on remote hosts.
22 *
23 * <p>This is not an exact science; a best effort is made.
24 *
25 * @author Mark Taylor
26 * @since 29 Jul 2009
27 */
28 class UrlExporter {
29
30 private final String host_;
31 private final boolean exportFiles_;
32 private static final Logger logger_ =
33 Logger.getLogger( UrlExporter.class.getName() );
34 private static final Pattern LOCALHOST_REGEX =
35 Pattern.compile( "(http://|ftp://)"
36 + "(127\\.0\\.0\\.1|localhost)"
37 + "([:/].*)" );
38 private static final Pattern FILE_REGEX =
39 Pattern.compile( "(file://)"
40 + "([^/]*)"
41 + "/.*" );
42
43 /**
44 * Constructor.
45 *
46 * @param host public name of the host to which loopback addresses
47 * refer
48 * @param exportFiles whether to export file-protocol URLs
49 * by turning them into http ones;
50 * this only makes sense if the current JVM
51 * is running on a machine which can see
52 * <code>host</code>'s filesystem(s)
53 */
54 public UrlExporter( String host, boolean exportFiles ) {
55 host_ = host;
56 exportFiles_ = exportFiles;
57 }
58
59 /**
60 * Exports a single string for remote usage.
61 * If it looks like a URL, it's changed. Not foolproof.
62 *
63 * @param text string to assess
64 * @return copy of text if it's not a URL, otherwise a possibly
65 * edited URL with the same content
66 */
67 public String exportString( String text ) {
68 String t2 = doExportString( text );
69 if ( t2 != null && ! t2.equals( text ) ) {
70 logger_.info( "Exported string \"" + text + "\" -> \"" + t2 + '"' );
71 }
72 return t2;
73 }
74
75 /**
76 * Does the work for {@link #exportString}.
77 *
78 * @param text string to assess
79 * @return copy of text if it's not a URL, otherwise a URL with a
80 * possibly edited host part
81 */
82 private String doExportString( String text ) {
83 Matcher localMatcher = LOCALHOST_REGEX.matcher( text );
84 if ( localMatcher.matches() ) {
85 return localMatcher.group( 1 ) + host_ + localMatcher.group( 3 );
86 }
87 else if ( exportFiles_ && FILE_REGEX.matcher( text ).matches() ) {
88 try {
89 URL fileUrl = new URL( text );
90 String path = fileUrl.getPath();
91 if ( File.separatorChar != '/' ) {
92 path = path.replace( '/', File.separatorChar );
93 }
94 File file = new File( path );
95 if ( file.canRead() && ! file.isDirectory() ) {
96 URL expUrl = UtilServer.getInstance()
97 .getMapperHandler()
98 .addLocalUrl( fileUrl );
99 if ( expUrl != null ) {
100 return expUrl.toString();
101 }
102 }
103 }
104 catch ( MalformedURLException e ) {
105 // not a URL at all - don't attempt to export it
106 }
107 catch ( IOException e ) {
108 // something else went wrong - leave alone
109 }
110 return text;
111 }
112 else {
113 return text;
114 }
115 }
116
117 /**
118 * Exports a list for remote usage by changing its contents in place.
119 *
120 * @param list list to edit
121 */
122 public void exportList( List list ) {
123 for ( ListIterator it = list.listIterator(); it.hasNext(); ) {
124 Object value = it.next();
125 if ( value instanceof String ) {
126 it.set( exportString( (String) value ) );
127 }
128 else if ( value instanceof List ) {
129 exportList( (List) value );
130 }
131 else if ( value instanceof Map ) {
132 exportMap( (Map) value );
133 }
134 }
135 }
136
137 /**
138 * Exports a map for remote usage by changing its contents in place.
139 *
140 * @param map map to edit
141 */
142 public void exportMap( Map map ) {
143 for ( Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
144 Map.Entry entry = (Map.Entry) it.next();
145 Object value = entry.getValue();
146 if ( value instanceof String ) {
147 entry.setValue( exportString( (String) value ) );
148 }
149 else if ( value instanceof List ) {
150 exportList( (List) value );
151 }
152 else if ( value instanceof Map ) {
153 exportMap( (Map) value );
154 }
155 }
156 }
157 }