Java URL Decode decodeIfNeeded(String s)

Here you can find the source of decodeIfNeeded(String s)

Description

Unfortunately we have code that mindlessly decoded URLs (FileDownloadWindow) which borked (in the case I discovered) magnet uris with encoded parameters (e.g.

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration


public static String decodeIfNeeded(String s) 

Method Source Code

//package com.java2s;
/*/* w ww.  j  av  a 2s. c o m*/
 * Created on Mar 21, 2006 3:09:00 PM
 * Copyright (C) Azureus Software, Inc, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.io.UnsupportedEncodingException;
import java.net.*;

public class Main {
    /**
     * Unfortunately we have code that mindlessly decoded URLs (FileDownloadWindow) which borked (in the case I discovered) magnet uris with encoded
     * parameters (e.g. the &tr= parameter) - doing so screws stuff up later if, for example, the parameter included an encoded '&'
     * @param s
     * @return
     */

    public static String decodeIfNeeded(String s) {
        if (s == null) {

            return ("");
        }

        try {
            // of course someone prolly added the blind URLDecode for a reason so try and just deal with the borkage
            // which means looking for &a=b elements and ensure we don't URLDecode the 'b'

            // only have issues if there's a naked '?' there

            int q_pos = s.indexOf('?');
            int a_pos = s.indexOf('&');

            if (q_pos == -1 && a_pos == -1) {

                return (decode(s));
            }

            int start = Math.min(q_pos, a_pos);

            return (decode(s.substring(0, start)) + s.substring(start));

        } catch (Throwable e) {

            return (s);
        }
    }

    public static String decode(String s) {
        if (s == null) {
            return "";
        }
        try {
            try {
                return (URLDecoder.decode(s, "UTF-8"));

            } catch (IllegalArgumentException e) {

                // handle truncated encodings somewhat gracefully

                int pos = s.lastIndexOf("%");

                if (pos >= s.length() - 2) {

                    return (URLDecoder.decode(s.substring(0, pos), "UTF-8"));
                }

                throw (e);
            }
        } catch (UnsupportedEncodingException e) {

            return (URLDecoder.decode(s));
        }
    }
}

Related

  1. decodeAjaxParam(final String source)
  2. decodeArray(final String val)
  3. decodeCodedStr(String sourceStr, String targetCharset)
  4. decodeFormData(String formData)
  5. decodeHtmlString(String inputString)
  6. decodeInternally(String encoded)
  7. decodeJobHistoryFileName(String logFileName)
  8. decodeKVMap(String keyValueString)
  9. decodeName(final String name)