|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2015 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.io; |
| 33 | + |
| 34 | +import java.io.UnsupportedEncodingException; |
| 35 | +import java.net.URI; |
| 36 | +import java.net.URISyntaxException; |
| 37 | +import java.net.URLDecoder; |
| 38 | +import java.util.LinkedHashMap; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +import org.scijava.log.LogService; |
| 42 | +import org.scijava.plugin.Parameter; |
| 43 | + |
| 44 | +/** |
| 45 | + * {@link Location} backed by a {@link URI} string. |
| 46 | + * |
| 47 | + * @author Curtis Rueden |
| 48 | + */ |
| 49 | +public class URILocation extends AbstractLocation { |
| 50 | + |
| 51 | + @Parameter |
| 52 | + private LogService log; |
| 53 | + |
| 54 | + private final URI uri; |
| 55 | + |
| 56 | + public URILocation(final URI uri) { |
| 57 | + this.uri = uri; |
| 58 | + } |
| 59 | + |
| 60 | + public URILocation(final String uriPath) throws URISyntaxException { |
| 61 | + this(new URI(uriPath)); |
| 62 | + } |
| 63 | + |
| 64 | + // -- URILocation methods -- |
| 65 | + |
| 66 | + public Map<String, String> getQueryMap() { |
| 67 | + return decodeQuery(getURI().getRawQuery()); |
| 68 | + } |
| 69 | + |
| 70 | + public String getQueryValue(final String key) { |
| 71 | + return getQueryMap().get(key); |
| 72 | + } |
| 73 | + |
| 74 | + // FIXME: look up whether anyone has created a mutatable URI class, |
| 75 | + // with individual setters for the various parts. Otherwise, we'll |
| 76 | + // have to handle it here! |
| 77 | + |
| 78 | + // -- Location methods -- |
| 79 | + |
| 80 | + @Override |
| 81 | + public URI getURI() { |
| 82 | + return uri; |
| 83 | + } |
| 84 | + |
| 85 | + // -- Helper methods -- |
| 86 | + |
| 87 | + /** |
| 88 | + * Decodes a query string of ampersand-separated key/value pairs. E.g.: |
| 89 | + * {@code apples=yummy&bananas=delicious&grapefruits=scrumptious}. |
| 90 | + * |
| 91 | + * @param query The query string to decode. |
| 92 | + * @return A map of the decoded key/value pairs. |
| 93 | + */ |
| 94 | + private Map<String, String> decodeQuery(final String query) { |
| 95 | + final Map<String, String> map = new LinkedHashMap<String, String>(); |
| 96 | + if (query == null) return map; |
| 97 | + for (final String param : query.split("&")) { |
| 98 | + final int equals = param.indexOf("="); |
| 99 | + if (equals < 0) { |
| 100 | + map.put(decode(param), "true"); |
| 101 | + } |
| 102 | + else { |
| 103 | + final String key = decode(param.substring(0, equals)); |
| 104 | + final String value = decode(param.substring(equals + 1)); |
| 105 | + map.put(key, value); |
| 106 | + } |
| 107 | + } |
| 108 | + return map; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Decodes a single uuencoded string. |
| 113 | + * |
| 114 | + * @see URLDecoder |
| 115 | + */ |
| 116 | + private String decode(final String s) { |
| 117 | + // http://stackoverflow.com/a/6926987 |
| 118 | + try { |
| 119 | + return URLDecoder.decode(s.replace("+", "%2B"), "UTF-8"); |
| 120 | + } |
| 121 | + catch (UnsupportedEncodingException exc) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments