Это моя вторая прога на джаве. Готов выслушать любую критику кроме "Да это же blob", "Где неймспейсы?" и "Тут нет javadoc". Конкретнее интересует, правильно ли использую исключения.
| Код | import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException;
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map;
class ApiClientException extends Exception { }
class MalformedResponseException extends ApiClientException { }
class NoUserException extends ApiClientException { }
class AccessDeniedException extends ApiClientException { }
class IncorrectEmailException extends ApiClientException { }
public class ApiClient { protected String apiUrl; JSONParser parser;
ApiClient(String apiUrl) { this.apiUrl = apiUrl; parser = new JSONParser(); }
protected Object makeRequest(List<NameValuePair> params) throws IOException, MalformedResponseException { DefaultHttpClient httpclient = new DefaultHttpClient(); try { String request = URLEncodedUtils.format(params, "UTF-8"); HttpGet httpget = new HttpGet(apiUrl + "?" + request); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity); try { return parser.parse(result); } catch (ParseException e) { throw new MalformedResponseException(); } } EntityUtils.consume(entity); } catch (ClientProtocolException e) { throw new MalformedResponseException(); } finally { httpclient.getConnectionManager().shutdown(); } return null; }
public String getSalt(String username) throws NoUserException, MalformedResponseException, IOException { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("api", "getSalt")); params.add(new BasicNameValuePair("username", username)); String response; try { response = (String) makeRequest(params); } catch (ClassCastException e) { throw new MalformedResponseException(); } if (response.length() == 0) { throw new NoUserException(); } return response; }
public String getToken(String salt, String hash) throws NoUserException, MalformedResponseException, IOException { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("api", "getToken")); params.add(new BasicNameValuePair("salt", salt)); params.add(new BasicNameValuePair("hash", hash)); String response; try { response = (String) makeRequest(params); } catch (ClassCastException e) { throw new MalformedResponseException(); } if (response.length() == 0) { throw new NoUserException(); } return response; }
public Map auth(String token) throws NoUserException, MalformedResponseException, IOException { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("api", "auth")); params.add(new BasicNameValuePair("token", token)); Map response; try { response = (Map) makeRequest(params); } catch (ClassCastException e) { throw new MalformedResponseException(); } if (response == null) { throw new NoUserException(); } return response; }
public void userCreate(String token, String newUserName, String newPassword, String newEmail) throws MalformedResponseException, IOException, AccessDeniedException, IncorrectEmailException { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("api", "userCreate")); params.add(new BasicNameValuePair("token", token)); params.add(new BasicNameValuePair("username", newUserName)); params.add(new BasicNameValuePair("password", newPassword)); params.add(new BasicNameValuePair("email", newEmail)); String response; try { response = (String) makeRequest(params); } catch (ClassCastException e) { throw new MalformedResponseException(); } if (response.equals("Access denied")) { throw new AccessDeniedException(); } if (response.equals("Incorrect Email")) { throw new IncorrectEmailException(); } if (!response.equals("OK")) { throw new MalformedResponseException(); } }
}
|
P.S. Где про дектораторы(@something перед функциями) почитать можно? желательно подробно и предпочтительно на русском  |