Project

General

Profile

Download (6.77 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / hesiod / javaExt / src / fr / ias / sitools / astro / resolverName / resource / CorotIdResolverAstero.java @ 6552a8ce

1
 /*******************************************************************************
2
 * Copyright 2010-2013 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
3
 *
4
 * This file is part of SITools2.
5
 *
6
 * SITools2 is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * SITools2 is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with SITools2.  If not, see <http://www.gnu.org/licenses/>.
18
 ******************************************************************************/
19
package fr.ias.sitools.astro.resolverName.resource;
20

    
21
import fr.cnes.sitools.astro.resolver.AbstractNameResolver;
22
import fr.cnes.sitools.astro.resolver.NameResolverException;
23
import fr.cnes.sitools.astro.resolver.NameResolverResponse;
24
import fr.cnes.sitools.util.ClientResourceProxy;
25
import java.io.IOException;
26
import java.util.logging.Level;
27
import java.util.logging.Logger;
28
import org.json.JSONArray;
29
import org.json.JSONException;
30
import org.json.JSONObject;
31
import org.restlet.Client;
32
import org.restlet.data.Method;
33
import org.restlet.data.Protocol;
34
import org.restlet.data.Status;
35
import org.restlet.resource.ClientResource;
36
import org.restlet.resource.ResourceException;
37

    
38
/**
39
 * Queries the Corot name resolver and returns the list of coordinates for a given identifier.<br/> A CorotIdResolver lets you get a sky
40
 * position given a Corot identifier
41
 *
42
 * @author Marc NICOLAS <marc.nicolas@ias.u-psud.fr>
43
 */
44
public class CorotIdResolverAstero extends AbstractNameResolver {
45

    
46
  /**
47
   * Logger.
48
   */
49
  private static final Logger LOG = Logger.getLogger(CorotIdResolverAstero.class.getName());
50
  /**
51
   * Credits to return for CDS.
52
   */
53
  private static final String CREDITS_NAME = "IAS Astero/CNES";
54
  /**
55
   * Template URL for the Corot identifier resolver service.
56
   */
57
  private static final String TEMPLATE_NAME_RESOLVER = "http://idoc-corotn2-public-v2.ias.u-psud.fr/ds/astero/plugin/corotIdResolver/EQUATORIAL/<corotid>";
58
  /**
59
   * Corot service response.
60
   */
61
  private String corotId;
62

    
63
  /**
64
   * Empty constructor.
65
   */
66
  protected CorotIdResolverAstero() {
67
  }
68

    
69
  /**
70
   * Constructs a new CorotId resolver.
71
   *
72
   * @param corotIdVal Corot ID
73
   */
74
  public CorotIdResolverAstero(final String corotIdVal) {
75
    setCorotId(corotIdVal);
76
    checkInputParameters();      
77
  }
78
  /**
79
   * Sets the Corot Id.
80
   * @param corotIdVal the Corot ID to set
81
   */
82
  protected final void setCorotId(final String corotIdVal) {
83
      this.corotId = corotIdVal;
84
  }
85
  /**
86
   * Returns the Corot ID.
87
   * @return the Corot ID
88
   */
89
  protected final String getCorotId() {
90
      return this.corotId;
91
  }
92
  /**
93
   * Tests if the coroID is set.
94
   *
95
   * <p>
96
   * Returns IllegalArgumentException if <code>corotId</code> is <code>null</code> or empty.
97
   * </p>
98
   */
99
  protected final void checkInputParameters() {
100
    if (getCorotId() == null || getCorotId().isEmpty()) {
101
      throw new IllegalArgumentException("corotId must be set.");
102
    }
103
  }
104

    
105
  @Override
106
  public final NameResolverResponse getResponse() {
107
    NameResolverResponse response = new NameResolverResponse(CREDITS_NAME);
108
    LOG.log(Level.SEVERE, "JE RENTRE DANS LE NAMERESOLVERRESPONSE de l'Astero !!");
109
    try {
110
      final String query = TEMPLATE_NAME_RESOLVER.replace("<corotid>", corotId);   
111
      final JSONObject json = parseResponse(query);
112

    
113
      final String[] coordinates = parseCoordinates(json);
114
      final double rightAscension = Double.valueOf(coordinates[0]);
115
      final double declination = Double.valueOf(coordinates[1]);
116
      
117
      response.addAstroCoordinate(rightAscension, declination);
118
    } catch (NameResolverException ex) {
119
      if (getSuccessor() == null) {
120
        response.setError(ex);        
121
      } else {
122
        response = getSuccessor().getResponse();
123
      }
124
    } finally {
125
      return response;
126
    }
127
  }
128

    
129
  /**
130
   * Queries the SITools2 service at IAS and stores the result in <code>json</code>.
131
   *
132
   * @param query query the Corot service
133
   * @return the response from the server
134
   * @throws NameResolverException if a problem occurs while the response is being parsed
135
   */
136
  private JSONObject parseResponse(final String query) throws NameResolverException {
137
    assert query != null;
138
    LOG.log(Level.INFO, "Call IAS name resolver: {0}", query);
139
    final ClientResourceProxy proxy = new ClientResourceProxy(query, Method.GET);
140
    final ClientResource client = proxy.getClientResource();
141
    //client.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC, "guest", "sitools2public"));
142
    final Client clientHTTP = new Client(Protocol.HTTP);
143
    clientHTTP.setConnectTimeout(AbstractNameResolver.SERVER_TIMEOUT);
144
    client.setNext(clientHTTP);
145
    final Status status = client.getStatus();
146
    if (status.isSuccess()) {
147
      JSONObject json;
148
      try {
149
        json = new JSONObject(client.get().getText());
150
      } catch (IOException ex) {
151
        throw new NameResolverException(Status.SERVER_ERROR_INTERNAL, ex);
152
      } catch (JSONException ex) {
153
        LOG.log(Level.WARNING, "the response of Corot server may changed");
154
        throw new NameResolverException(Status.CLIENT_ERROR_NOT_ACCEPTABLE, ex);
155
      } catch (ResourceException ex) {
156
        throw new NameResolverException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, ex);
157
      }
158
      return json;
159
    } else {
160
      throw new NameResolverException(status, status.getThrowable());
161
    }
162
  }
163

    
164
  /**
165
   * Parses the coordinates from CDS response and return them.
166
   * @param json the server's response
167
   * @return the following array [rightAscension,declination]
168
   * @throws NameResolverException - if empty response from Corot
169
   */
170
  private String[] parseCoordinates(final JSONObject json) throws NameResolverException {
171
    try {
172
      //final JSONArray jsonArray = json.getJSONArray("data");
173
      final JSONArray jsonArray = json.getJSONArray("features");
174
      final JSONArray coords  = jsonArray.getJSONObject(0).getJSONObject("geometry").getJSONArray("coordinates");
175
      
176
      if (jsonArray.length() != 1) {
177
        throw new NameResolverException(Status.CLIENT_ERROR_NOT_FOUND, "Not found");
178
      }
179
      
180
      if (coords.get(0).equals("") || coords.get(1).toString().equals("")) {
181
        throw new NameResolverException(Status.CLIENT_ERROR_NOT_FOUND, "Not found");
182
      }
183
      return new String[]{coords.get(0).toString(),coords.get(1).toString()};
184
    } catch (JSONException ex) {
185
      throw new NameResolverException(Status.SERVER_ERROR_INTERNAL, "cannot parse the coordinates");
186
    }
187
  }
188
}