Browse Source

Tidy up downloader code

master
Chris Smith 15 years ago
parent
commit
9db70dddf1

+ 1
- 1
nbproject/coverage.properties View File

1
 #coverage viewer module properties for project
1
 #coverage viewer module properties for project
2
-#Tue Jan 06 09:54:06 GMT 2009
2
+#Tue Jan 06 10:02:47 GMT 2009
3
 coverage.activity=ON
3
 coverage.activity=ON
4
 project.type=java
4
 project.type=java
5
 coverage.templateFile=coverage/template.emma
5
 coverage.templateFile=coverage/template.emma

+ 1
- 1
nbproject/project.properties View File

73
 run.test.classpath=\
73
 run.test.classpath=\
74
     ${javac.test.classpath}:\
74
     ${javac.test.classpath}:\
75
     ${build.test.classes.dir}:\
75
     ${build.test.classes.dir}:\
76
-    ${file.reference.emma.jar}
76
+    :${file.reference.emma.jar}
77
 source.encoding=UTF-8
77
 source.encoding=UTF-8
78
 src.dir=src
78
 src.dir=src
79
 test.src.dir=test
79
 test.src.dir=test

+ 8
- 19
src/uk/co/md87/evetool/api/io/ApiDownloader.java View File

69
 
69
 
70
         final CacheStatus cacheStatus = cache.getCacheStatus(method, args);
70
         final CacheStatus cacheStatus = cache.getCacheStatus(method, args);
71
 
71
 
72
-        if (cacheStatus == CacheStatus.HIT) {
73
-            return cache.getCache(method, args);
74
-        }
75
-
76
-        try {
77
-            final StringBuilder builder = new StringBuilder();
78
-            for (String line : Downloader.getPage(getUrl(method), ourArgs)) {
79
-                builder.append(line);
80
-            }
81
-
82
-            cache.setCache(method, args, builder.toString(),
83
-                    System.currentTimeMillis() + 20000); // TODO: Proper time
84
-            return cache.getCache(method, args);
85
-        } catch (IOException ex) {
86
-            LOGGER.log(Level.WARNING, "API request failed", ex);
87
-            if (cacheStatus == CacheStatus.EXPIRED) {
88
-                return cache.getCache(method, args);
89
-            } else {
90
-                return null;
72
+        if (cacheStatus == CacheStatus.MISS || cacheStatus == CacheStatus.EXPIRED) {
73
+            try {
74
+                cache.setCache(method, args, Downloader.getPage(getUrl(method), ourArgs),
75
+                        System.currentTimeMillis() + 20000); // TODO: Proper time
76
+            } catch (IOException ex) {
77
+                LOGGER.log(Level.WARNING, "API request failed", ex);
91
             }
78
             }
92
         }
79
         }
80
+
81
+        return cache.getCache(method, args);
93
     }
82
     }
94
 
83
 
95
     protected static String getUrl(final String method) {
84
     protected static String getUrl(final String method) {

+ 10
- 8
src/uk/co/md87/evetool/api/io/Downloader.java View File

58
      * @throws java.net.MalformedURLException If the URL is malformed
58
      * @throws java.net.MalformedURLException If the URL is malformed
59
      * @throws java.io.IOException If there's an I/O error while downloading
59
      * @throws java.io.IOException If there's an I/O error while downloading
60
      */
60
      */
61
-    public static List<String> getPage(final String url)
61
+    public static String getPage(final String url)
62
             throws MalformedURLException, IOException {
62
             throws MalformedURLException, IOException {
63
         
63
         
64
         return getPage(url, "");
64
         return getPage(url, "");
66
 
66
 
67
     /**
67
     /**
68
      * Retrieves the specified page, sending the specified post data.
68
      * Retrieves the specified page, sending the specified post data.
69
-     * 
69
+     *
70
+     * TODO: Update
70
      * @param url The URL to retrieve
71
      * @param url The URL to retrieve
71
      * @param postData The raw POST data to send
72
      * @param postData The raw POST data to send
72
      * @return A list of lines received from the server
73
      * @return A list of lines received from the server
73
      * @throws java.net.MalformedURLException If the URL is malformed
74
      * @throws java.net.MalformedURLException If the URL is malformed
74
      * @throws java.io.IOException If there's an I/O error while downloading
75
      * @throws java.io.IOException If there's an I/O error while downloading
75
      */    
76
      */    
76
-    public static List<String> getPage(final String url, final String postData)
77
+    public static String getPage(final String url, final String postData)
77
             throws MalformedURLException, IOException {
78
             throws MalformedURLException, IOException {
78
         
79
         
79
-        final List<String> res = new ArrayList<String>();
80
+        final StringBuilder res = new StringBuilder();
80
         
81
         
81
         final URLConnection urlConn = getConnection(url, postData);
82
         final URLConnection urlConn = getConnection(url, postData);
82
         
83
         
89
             line = in.readLine();
90
             line = in.readLine();
90
             
91
             
91
             if (line != null) {
92
             if (line != null) {
92
-                res.add(line);
93
+                res.append(line);
93
             }
94
             }
94
         } while (line != null);
95
         } while (line != null);
95
         
96
         
96
         in.close();
97
         in.close();
97
         
98
         
98
-        return res;
99
+        return res.toString();
99
     }
100
     }
100
     
101
     
101
     /**
102
     /**
102
      * Retrieves the specified page, sending the specified post data.
103
      * Retrieves the specified page, sending the specified post data.
103
-     * 
104
+     *
105
+     * TODO: Update
104
      * @param url The URL to retrieve
106
      * @param url The URL to retrieve
105
      * @param postData A map of post data that should be sent
107
      * @param postData A map of post data that should be sent
106
      * @return A list of lines received from the server
108
      * @return A list of lines received from the server
107
      * @throws java.net.MalformedURLException If the URL is malformed
109
      * @throws java.net.MalformedURLException If the URL is malformed
108
      * @throws java.io.IOException If there's an I/O error while downloading
110
      * @throws java.io.IOException If there's an I/O error while downloading
109
      */    
111
      */    
110
-    public static List<String> getPage(final String url, final Map<String, String> postData)
112
+    public static String getPage(final String url, final Map<String, String> postData)
111
             throws MalformedURLException, IOException {        
113
             throws MalformedURLException, IOException {        
112
         return getPage(url, encodeArguments(postData));
114
         return getPage(url, encodeArguments(postData));
113
     }
115
     }

+ 1
- 0
src/uk/co/md87/evetool/api/parser/ApiParser.java View File

8
 import java.io.IOException;
8
 import java.io.IOException;
9
 import java.io.StringReader;
9
 import java.io.StringReader;
10
 import java.util.List;
10
 import java.util.List;
11
+
11
 import org.jdom.Document;
12
 import org.jdom.Document;
12
 import org.jdom.Element;
13
 import org.jdom.Element;
13
 import org.jdom.JDOMException;
14
 import org.jdom.JDOMException;

Loading…
Cancel
Save