|
3 | 3 | */ |
4 | 4 | package edu.caltech.ipac.astro.net; |
5 | 5 |
|
| 6 | +import edu.caltech.ipac.firefly.messaging.JsonHelper; |
6 | 7 | import edu.caltech.ipac.util.AppProperties; |
7 | 8 | import edu.caltech.ipac.util.StringUtils; |
8 | 9 | import edu.caltech.ipac.util.download.FailedRequestException; |
9 | 10 | import edu.caltech.ipac.util.download.URLDownload; |
| 11 | +import org.json.simple.JSONArray; |
| 12 | +import org.json.simple.JSONObject; |
10 | 13 |
|
11 | | -import java.io.BufferedReader; |
12 | 14 | import java.io.IOException; |
13 | 15 | import java.io.Serializable; |
14 | | -import java.io.StringReader; |
15 | 16 | import java.net.URL; |
16 | 17 | import java.net.URLEncoder; |
17 | 18 | import java.util.ArrayList; |
18 | | -import java.util.HashMap; |
19 | | -import java.util.Iterator; |
20 | 19 | import java.util.List; |
21 | | -import java.util.Map; |
22 | 20 |
|
23 | | -/** |
24 | | - * @author Booth Hartley |
25 | | - * @version $Id: HorizonsEphPairs.java,v 1.12 2012/03/13 22:23:02 roby Exp $ |
26 | | - */ |
27 | 21 | public class HorizonsEphPairs { |
28 | 22 |
|
29 | 23 | public static final String horizonsServer = AppProperties.getProperty("horizons.host", "https://ssd.jpl.nasa.gov"); |
30 | | - |
31 | | - private static final String PARSE_ERROR= "Error Parsing Name Resolver results"; |
32 | | - |
33 | | - private static final String NAME_IDENT = "Object Name"; |
34 | | - private static final String ID_IDENT = "Primary SPKID"; |
35 | | - private static final String DES_IDENT = "Primary designation"; |
36 | | - private static final String ALIAS_IDENT = "Aliases"; |
37 | | - |
38 | | - // private static final String CGI_CMD= "/cgi-bin/smb_spk.cgi"; |
39 | | - private static final String CGI_CMD = "/x/smb_spk.cgi"; |
40 | | - |
41 | | - |
| 24 | + public static final String path= "/api/horizons_lookup.api"; |
42 | 25 |
|
43 | 26 | public static HorizonsResults[] lowlevelGetEphInfo(String idOrName) throws FailedRequestException { |
44 | 27 |
|
45 | 28 | boolean isName = true; |
46 | | - HorizonsResults retval[] = null; |
47 | 29 | try { |
48 | 30 | Integer.parseInt(idOrName); |
49 | 31 | isName = false; |
50 | | - } catch (NumberFormatException e) { |
51 | | - } |
| 32 | + } catch (NumberFormatException ignore) { } |
52 | 33 |
|
53 | 34 | if (isName) { |
54 | 35 | idOrName = StringUtils.crunch(idOrName); |
55 | | - String s[] = idOrName.split(" "); |
| 36 | + String[] s = idOrName.split(" "); |
56 | 37 | if (s.length >= 2) { |
57 | 38 | try { |
58 | 39 | Integer.parseInt(s[0]); |
59 | 40 | s = idOrName.split("^[0-9]* "); // now get the rest |
60 | 41 | if (inParens(s[1])) { |
61 | 42 | idOrName = stripFirstLast(s[1]); |
62 | 43 | } else { |
63 | | - char cAry[] = s[1].toCharArray(); |
| 44 | + char[] cAry = s[1].toCharArray(); |
64 | 45 | boolean hasDigit = false; |
65 | 46 | for (int i = 0; (i < cAry.length && !hasDigit); i++) { |
66 | 47 | hasDigit = Character.isDigit(cAry[i]); |
67 | 48 | } |
68 | 49 | if (!hasDigit) idOrName = s[1]; |
69 | 50 | } |
70 | | - } catch (NumberFormatException e) { |
| 51 | + } catch (NumberFormatException ignore) { |
71 | 52 | } |
72 | 53 | } |
73 | 54 | } |
74 | 55 |
|
75 | | - Map<String,String> data = new HashMap<>(); |
76 | 56 |
|
77 | 57 | try { |
78 | | - data.put("OPTION", "Look up"); |
79 | | - data.put("OBJECT",URLEncoder.encode(idOrName)); |
80 | | - |
81 | | - String urlStr = horizonsServer + CGI_CMD; |
| 58 | + String urlStr = horizonsServer + path + "?sstr="+ URLEncoder.encode(idOrName, "UTF-8"); |
82 | 59 | URL url = new URL(urlStr); |
83 | | - String line; |
84 | | - String result = URLDownload.getDataFromURL(url, data,null,null).getResultAsString(); |
85 | | - BufferedReader rd = new BufferedReader(new StringReader(result)); |
86 | | - |
87 | | - |
88 | | - List<String> list = new ArrayList<String>(12); |
89 | | - boolean error = false; |
90 | | - while ((line = rd.readLine()) != null) { |
91 | | - list.add(line); |
92 | | - if (line.indexOf("ERROR") > -1) error = true; |
| 60 | + String jsonStrResult = URLDownload.getDataFromURL(url, null,null,null).getResultAsString(); |
| 61 | + JsonHelper json= JsonHelper.parse(jsonStrResult); |
| 62 | + |
| 63 | + JSONArray rList= json.getValue(new JSONArray(), "result"); |
| 64 | + List<HorizonsResults> horizonsResults= new ArrayList<>(); |
| 65 | + for(Object r : rList) { |
| 66 | + String pdes= (String)((JSONObject)r).get("pdes"); |
| 67 | + String name= (String)((JSONObject)r).get("name"); |
| 68 | + String naifID= (String)((JSONObject)r).get("spkid"); |
| 69 | + JSONArray aList = (JSONArray)((JSONObject)r).get("alias"); |
| 70 | + String[] aStrAry= (String[])aList.toArray(new String[0]); |
| 71 | + horizonsResults.add(new HorizonsResults(name,naifID,pdes,aStrAry)); |
93 | 72 | } |
94 | | - rd.close(); |
95 | | - |
96 | | - |
97 | | - if (error) { |
98 | | - StringBuffer errStrBuff = new StringBuffer(100); |
99 | | - errStrBuff.append("<html>"); |
100 | | - for (String s : list) errStrBuff.append(s); |
101 | | - throw new FailedRequestException("<html>" + |
102 | | - "<br><br><b>ERROR:</b><br><br>" + |
103 | | - "No matches found for: " + idOrName); |
104 | | - } |
105 | | - |
106 | | - retval = buildHorizonsResults(list); |
| 73 | + return horizonsResults.toArray(new HorizonsResults[0]); |
107 | 74 | } catch (IOException e) { |
108 | | - throw new FailedRequestException("failed to retrieve ephermeris pairs", |
109 | | - "IOException in getting ephermeris info", e); |
| 75 | + throw new FailedRequestException("failed to retrieve ephemeris pairs", |
| 76 | + "IOException in getting ephemeris info", e); |
110 | 77 | } |
111 | | - return retval; |
112 | 78 | } |
113 | 79 |
|
114 | 80 |
|
115 | | - private static String appendAll(List l) { |
116 | | - StringBuffer buff = new StringBuffer(100); |
117 | | - for (Iterator i = l.iterator(); i.hasNext(); ) { |
118 | | - buff.append(i.next().toString()); |
119 | | - buff.append("\n"); |
120 | | - } |
121 | | - return buff.toString(); |
122 | | - } |
123 | | - |
124 | | - |
125 | | - private static HorizonsResults[] buildHorizonsResults(List<String> list) |
126 | | - throws FailedRequestException { |
127 | | - |
128 | | - String label; |
129 | | - String sAry[]; |
130 | | - String name = null; |
131 | | - String id = null; |
132 | | - String des = null; |
133 | | - String alias = null; |
134 | | - String aliases[] = null; |
135 | | - List retList = new ArrayList(4); |
136 | | - for (String s : list) { |
137 | | - if (s.length() == 0) { |
138 | | - |
139 | | - if (name == null || id == null || des == null || aliases == null) { |
140 | | - throw new FailedRequestException( |
141 | | - PARSE_ERROR, |
142 | | - "One of the fields was null.\n" + |
143 | | - "Results from query:\n" + |
144 | | - appendAll(list)); |
145 | | - } else { |
146 | | - if (StringUtils.isEmpty(name)) { |
147 | | - name = StringUtils.isEmpty(des) ? "No Name*" : des; |
148 | | - } |
149 | | - retList.add(new HorizonsResults(name, id, des, aliases)); |
150 | | - } |
151 | | - name = null; |
152 | | - id = null; |
153 | | - des = null; |
154 | | - alias = null; |
155 | | - } else { |
156 | | - sAry = s.split("="); |
157 | | - if (sAry.length != 2) { |
158 | | - throw new FailedRequestException( |
159 | | - PARSE_ERROR, |
160 | | - "Missing a value pair combination\n" + |
161 | | - "Results from query:\n" + |
162 | | - appendAll(list)); |
163 | | - } |
164 | | - |
165 | | - label = StringUtils.crunch(sAry[0]); |
166 | | - if (label.equals(NAME_IDENT)) { |
167 | | - name = StringUtils.crunch(sAry[1]); |
168 | | - if (StringUtils.isEmpty(name)) { |
169 | | - name = ""; |
170 | | - } else if (inParens(name)) { |
171 | | - name = stripFirstLast(name); |
172 | | - } |
173 | | - } else if (label.equals(ID_IDENT)) { |
174 | | - id = StringUtils.crunch(sAry[1]); |
175 | | - } else if (label.equals(DES_IDENT)) { |
176 | | - des = StringUtils.crunch(sAry[1]); |
177 | | - } else if (label.equals(ALIAS_IDENT)) { |
178 | | - alias = StringUtils.crunch(sAry[1]); |
179 | | - if (alias.length() == 0) { |
180 | | - aliases = new String[0]; |
181 | | - } else { |
182 | | - aliases = alias.split(","); |
183 | | - for (int k = 0; k < aliases.length; k++) { |
184 | | - aliases[k] = aliases[k].trim(); |
185 | | - } |
186 | | - } |
187 | | - } else { |
188 | | - // if not one of the four ignore this line |
189 | | - } |
190 | | - } |
191 | | - } // end loop |
192 | | - if (retList.size() == 0) { |
193 | | - throw new FailedRequestException( |
194 | | - PARSE_ERROR, |
195 | | - "No error indicated but no results\n" + |
196 | | - "Results from query:\n" + |
197 | | - appendAll(list)); |
198 | | - } |
199 | | - |
200 | | - return (HorizonsResults[]) retList.toArray(new HorizonsResults[0]); |
201 | | - } |
202 | | - |
203 | 81 |
|
204 | 82 | public static class HorizonsResults implements Serializable { |
205 | | - private final String _name; |
206 | | - private final String _naifID; |
207 | | - private final String _primaryDes; |
208 | | - private final String _aliases[]; |
209 | | - |
210 | | - public HorizonsResults(String name, String naifID, String primaryDes, |
211 | | - String aliases[]) { |
212 | | - _name = name; |
213 | | - _naifID = naifID; |
214 | | - _primaryDes = primaryDes; |
215 | | - _aliases = aliases; |
216 | | - } |
217 | | - |
218 | | - public final String getName() { |
219 | | - return _name; |
220 | | - } |
221 | | - |
222 | | - public final String getNaifID() { |
223 | | - return _naifID; |
| 83 | + private final String name; |
| 84 | + private final String naifID; |
| 85 | + private final String primaryDes; |
| 86 | + private final String [] aliases; |
| 87 | + |
| 88 | + public HorizonsResults(String name, String naifID, String primaryDes, String[] aliases) { |
| 89 | + this.name = name; |
| 90 | + this.naifID = naifID; |
| 91 | + this.primaryDes = primaryDes; |
| 92 | + this.aliases = aliases; |
224 | 93 | } |
225 | 94 |
|
226 | | - public final String getPrimaryDes() { |
227 | | - return _primaryDes; |
228 | | - } |
229 | | - |
230 | | - public final String[] getAliases() { |
231 | | - return _aliases; |
232 | | - } |
233 | | - |
234 | | - public String toString() { |
235 | | - return _name + " " + _naifID + " " + _primaryDes; |
236 | | - } |
| 95 | + public final String getName() { return name; } |
| 96 | + public final String getNaifID() { return naifID; } |
| 97 | + public final String getPrimaryDes() { return primaryDes; } |
| 98 | + public final String[] getAliases() { return aliases; } |
| 99 | + public String toString() { return name + " " + naifID + " " + primaryDes; } |
237 | 100 | } |
238 | 101 |
|
239 | 102 |
|
|
0 commit comments