Changeset 9227 in josm for trunk/src


Ignore:
Timestamp:
2015-12-31T03:24:56+01:00 (9 years ago)
Author:
Don-vip
Message:

OAuth: add robustness, basic unit test, code cleanup

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/oauth/signpost/AbstractOAuthProvider.java

    r6849 r9227  
    1212
    1313import java.io.BufferedReader;
     14import java.io.InputStream;
    1415import java.io.InputStreamReader;
    1516import java.util.HashMap;
     
    2829 * you will probably inherit from this class, since it takes a lot of work from
    2930 * you.
    30  * 
     31 *
    3132 * @author Matthias Kaeppler
    3233 */
     
    128129     * </ul>
    129130     * </p>
    130      * 
     131     *
    131132     * @param consumer
    132133     *        the {@link OAuthConsumer} that should be used to sign the request
     
    168169                consumer.setAdditionalParameters(customOAuthParams);
    169170            }
    170            
     171
    171172            if (this.listener != null) {
    172173                this.listener.prepareRequest(request);
     
    174175
    175176            consumer.sign(request);
    176            
     177
    177178            if (this.listener != null) {
    178179                this.listener.prepareSubmission(request);
     
    230231            return;
    231232        }
    232         BufferedReader reader = new BufferedReader(new InputStreamReader(response.getContent()));
    233233        StringBuilder responseBody = new StringBuilder();
    234 
    235         String line = reader.readLine();
    236         while (line != null) {
    237             responseBody.append(line);
    238             line = reader.readLine();
     234        InputStream content = response.getContent();
     235        if (content != null) {
     236            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
     237
     238            String line = reader.readLine();
     239            while (line != null) {
     240                responseBody.append(line);
     241                line = reader.readLine();
     242            }
    239243        }
    240244
     
    251255     * Overrride this method if you want to customize the logic for building a
    252256     * request object for the given endpoint URL.
    253      * 
     257     *
    254258     * @param endpointUrl
    255259     *        the URL to which the request will go
     
    263267     * Override this method if you want to customize the logic for how the given
    264268     * request is sent to the server.
    265      * 
     269     *
    266270     * @param request
    267271     *        the request to send
     
    275279     * Called when the connection is being finalized after receiving the
    276280     * response. Use this to do any cleanup / resource freeing.
    277      * 
     281     *
    278282     * @param request
    279283     *        the request that has been sent
     
    295299     * token reply. You must call {@link #setResponseParameters} with the set of
    296300     * parameters before using this method.
    297      * 
     301     *
    298302     * @param key
    299303     *        the parameter name
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmLoginFailedException.java

    r8510 r9227  
    22package org.openstreetmap.josm.gui.oauth;
    33
     4/**
     5 * OSM login failure exception.
     6 * @since 2746
     7 */
    48public class OsmLoginFailedException extends OsmOAuthAuthorizationException {
    59
    6     public OsmLoginFailedException() {
    7         super();
    8     }
    9 
    10     public OsmLoginFailedException(String arg0, Throwable arg1) {
    11         super(arg0, arg1);
    12     }
    13 
    14     public OsmLoginFailedException(String arg0) {
    15         super(arg0);
    16     }
    17 
    18     public OsmLoginFailedException(Throwable arg0) {
    19         super(arg0);
     10    /**
     11     * Constructs a new {@code OsmLoginFailedException} with the specified cause.
     12     * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
     13     */
     14    public OsmLoginFailedException(Throwable cause) {
     15        super(cause);
    2016    }
    2117}
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java

    r9172 r9227  
    5050        private String token;
    5151        private String userName;
    52     }
    53 
    54     /**
    55      * Creates a new authorisation client with default OAuth parameters
    56      *
    57      */
    58     public OsmOAuthAuthorizationClient() {
    59         oauthProviderParameters = OAuthParameters.createDefault(Main.pref.get("osm-server.url"));
    60         consumer = oauthProviderParameters.buildConsumer();
    61         provider = oauthProviderParameters.buildProvider(consumer);
    6252    }
    6353
     
    241231    }
    242232
    243     protected String buildPostRequest(Map<String, String> parameters) throws OsmOAuthAuthorizationException {
     233    protected static String buildPostRequest(Map<String, String> parameters) {
    244234        StringBuilder sb = new StringBuilder(32);
    245235
     
    322312     * Submits a request to the OSM website for a OAuth form. The OSM website replies a session token in
    323313     * a hidden parameter.
     314     * @param sessionId session id
     315     * @param requestToken request token
    324316     *
    325317     * @throws OsmOAuthAuthorizationException if something went wrong
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationException.java

    r3083 r9227  
    22package org.openstreetmap.josm.gui.oauth;
    33
     4/**
     5 * OSM OAuth authorization exception.
     6 * @since 2746
     7 */
    48public class OsmOAuthAuthorizationException extends Exception {
    59
    6     public OsmOAuthAuthorizationException() {
    7         super();
     10    /**
     11     * Constructs a new {@code OsmLoginFailedException} with the specified detail message.
     12     * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method.
     13     */
     14    public OsmOAuthAuthorizationException(String message) {
     15        super(message);
    816    }
    917
    10     public OsmOAuthAuthorizationException(String arg0, Throwable arg1) {
    11         super(arg0, arg1);
    12     }
    13 
    14     public OsmOAuthAuthorizationException(String arg0) {
    15         super(arg0);
    16     }
    17 
    18     public OsmOAuthAuthorizationException(Throwable arg0) {
    19         super(arg0);
     18    /**
     19     * Constructs a new {@code OsmLoginFailedException} with the specified cause.
     20     * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
     21     */
     22    public OsmOAuthAuthorizationException(Throwable cause) {
     23        super(cause);
    2024    }
    2125}
  • trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java

    r9185 r9227  
    1010import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    1111import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     12import org.openstreetmap.josm.tools.CheckParameterUtil;
    1213
    1314/**
     
    2324     * Constructs a new {@code ProgressInputStream}.
    2425     *
    25      * @param in the stream to monitor
     26     * @param in the stream to monitor. Must not be null
    2627     * @param size the total size which will be sent
    2728     * @param progressMonitor the monitor to report to
     
    2930     */
    3031    public ProgressInputStream(InputStream in, long size, ProgressMonitor progressMonitor) {
     32        CheckParameterUtil.ensureParameterNotNull(in, "in");
    3133        if (progressMonitor == null) {
    3234            progressMonitor = NullProgressMonitor.INSTANCE;
     
    4345     * @param con the connection to monitor
    4446     * @param progressMonitor the monitor to report to
     47     * @throws OsmTransferException if any I/O error occurs
    4548     */
    4649    public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9200 r9227  
    231231         * @see HttpURLConnection#getErrorStream()
    232232         */
     233        @SuppressWarnings("resource")
    233234        public InputStream getContent() throws IOException {
    234235            InputStream in;
     
    238239                in = connection.getErrorStream();
    239240            }
    240             in = new ProgressInputStream(in, getContentLength(), monitor);
    241             in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
    242             Compression compression = Compression.NONE;
    243             if (uncompress) {
    244                 final String contentType = getContentType();
    245                 Main.debug("Uncompressing input stream according to Content-Type header: {0}", contentType);
    246                 compression = Compression.forContentType(contentType);
    247             }
    248             if (uncompressAccordingToContentDisposition && Compression.NONE.equals(compression)) {
    249                 final String contentDisposition = getHeaderField("Content-Disposition");
    250                 final Matcher matcher = Pattern.compile("filename=\"([^\"]+)\"").matcher(contentDisposition);
    251                 if (matcher.find()) {
    252                     Main.debug("Uncompressing input stream according to Content-Disposition header: {0}", contentDisposition);
    253                     compression = Compression.byExtension(matcher.group(1));
     241            if (in != null) {
     242                in = new ProgressInputStream(in, getContentLength(), monitor);
     243                in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
     244                Compression compression = Compression.NONE;
     245                if (uncompress) {
     246                    final String contentType = getContentType();
     247                    Main.debug("Uncompressing input stream according to Content-Type header: {0}", contentType);
     248                    compression = Compression.forContentType(contentType);
    254249                }
    255             }
    256             in = compression.getUncompressedInputStream(in);
     250                if (uncompressAccordingToContentDisposition && Compression.NONE.equals(compression)) {
     251                    final String contentDisposition = getHeaderField("Content-Disposition");
     252                    final Matcher matcher = Pattern.compile("filename=\"([^\"]+)\"").matcher(contentDisposition);
     253                    if (matcher.find()) {
     254                        Main.debug("Uncompressing input stream according to Content-Disposition header: {0}", contentDisposition);
     255                        compression = Compression.byExtension(matcher.group(1));
     256                    }
     257                }
     258                in = compression.getUncompressedInputStream(in);
     259            }
    257260            return in;
    258261        }
Note: See TracChangeset for help on using the changeset viewer.