List of usage examples for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND
int SC_NOT_FOUND
To view the source code for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND.
Click Source Link
From source file:org.exoplatform.rest.client.openfire.ExoGroupProvider.java
public Group getGroup(String group) throws GroupNotFoundException { String url = groupInfoURL_;// ww w .j a v a2 s . c o m String method = groupInfoMethod_; HashMap<String, String> params = new HashMap<String, String>(groupInfoParams_); Response resp = null; url += group + "/"; try { if ("POST".equalsIgnoreCase(method)) { resp = Utils.doPost(new URL(url), params); } else if ("GET".equalsIgnoreCase(method)) { resp = Utils.doGet(new URL(url), params); } else throw new IllegalStateException( "Configuration error, only HTTP methods 'POST' or 'GET' are allowed, " + "but found '" + method + "'."); } catch (Exception e) { e.printStackTrace(); return null; } if (resp.getStatus() == HttpStatus.SC_OK) { Document d = resp.getResponseDoc(); String description = d.getDocumentElement().getElementsByTagName("description").item(0) .getTextContent(); NodeList t = d.getDocumentElement().getElementsByTagName("member"); List<JID> members = new ArrayList<JID>(); for (int i = 0; i < t.getLength(); i++) members.add(new JID(t.item(i).getTextContent())); // no administrators for each groups List<JID> administrators = Collections.emptyList(); return new Group(group, description, members, administrators); } else if (resp.getStatus() == HttpStatus.SC_NOT_FOUND) { throw new GroupNotFoundException(); } throw new IllegalStateException("Unknown response status : " + resp.getStatus()); }
From source file:org.exoplatform.rest.client.openfire.ExoGroupProvider.java
private String getGroupNameWithDescendants(String group) { String url = getGroupsAllURL_; String method = getGroupsAllMethod_; Response resp = null;//from w ww . ja va 2s . c o m HashMap<String, String> params = new HashMap<String, String>(); params.put("parentId", group); try { if ("POST".equalsIgnoreCase(method)) { resp = Utils.doPost(new URL(url), params); } else if ("GET".equalsIgnoreCase(method)) { resp = Utils.doGet(new URL(url), params); } else throw new IllegalStateException( "Configuration error, only HTTP methods 'POST' or 'GET' are allowed, " + "but found '" + method + "'."); } catch (Exception e) { e.printStackTrace(); return null; } if (resp.getStatus() == HttpStatus.SC_OK) { Document d = resp.getResponseDoc(); NodeList groupList = d.getDocumentElement().getElementsByTagName("group"); // recursion break if (groupList.getLength() == 0) return group; String compositeGroupName = group; for (int i = 0; i < groupList.getLength(); i++) { Node descendantGroup = groupList.item(i); NamedNodeMap attribs = descendantGroup.getAttributes(); String groupId = attribs.getNamedItem("groupId").getNodeValue(); compositeGroupName += ":" + getGroupNameWithDescendants(groupId); } return compositeGroupName; } else if (resp.getStatus() == HttpStatus.SC_NOT_FOUND) { throw new IllegalStateException("Group not found"); } throw new IllegalStateException("Unknown response status : " + resp.getStatus()); }
From source file:org.exoplatform.rest.client.openfire.ExoUserProvider.java
private Collection<String> findUsers(Set<String> fields, String query, String url, String method) { Response resp = null;/*ww w .j ava 2 s . c o m*/ HashMap<String, String> params = new HashMap<String, String>(findUsersParams_); for (String field : fields) { if ("name".equals(field)) { params.put("firstname", query); params.put("lastname", query); } else { params.put(field, query); } } try { if ("POST".equalsIgnoreCase(method)) resp = Utils.doPost(new URL(url), params); else if ("GET".equalsIgnoreCase(method)) resp = Utils.doGet(new URL(url), params); else throw new IllegalStateException( "Configuration error, only HTTP methods 'POST' or 'GET' are allowed, " + "but found '" + method + "'."); } catch (Exception e) { e.printStackTrace(); return null; } if (resp.getStatus() == HttpStatus.SC_OK) { return createUserList(resp.getResponseDoc()); } else if (resp.getStatus() == HttpStatus.SC_NOT_FOUND) { return Collections.emptyList(); } throw new IllegalStateException("Unknown response status : " + resp.getStatus()); }
From source file:org.exoplatform.rest.client.openfire.ExoUserProvider.java
public User loadUser(String username) throws UserNotFoundException { String url = userInfoURL_ + username + "/"; String method = userInfoMethod_; HashMap<String, String> params = new HashMap<String, String>(userInfoParams_); Response resp = null;//from w w w.j a va2s .c o m try { if ("POST".equalsIgnoreCase(method)) resp = Utils.doPost(new URL(url), params); else if ("GET".equalsIgnoreCase(method)) resp = Utils.doGet(new URL(url), params); else throw new IllegalStateException( "Configuration error, only HTTP methods 'POST' or 'GET' are allowed, " + "but found '" + method + "'."); } catch (Exception e) { e.printStackTrace(); return null; } if (resp.getStatus() == HttpStatus.SC_OK) { Document d = resp.getResponseDoc(); String name = d.getDocumentElement().getElementsByTagName("first-name").item(0).getTextContent(); String email = d.getDocumentElement().getElementsByTagName("email").item(0).getTextContent(); return new User(username, name, email, new Date(), new Date()); } else if (resp.getStatus() == HttpStatus.SC_NOT_FOUND) { throw new UserNotFoundException("User '" + username + "' not found!"); } throw new IllegalStateException("Unknown response status : " + resp.getStatus()); }
From source file:org.fao.geonet.doi.client.DoiClient.java
/** * This request marks a dataset as 'inactive'. * To activate it again, POST new metadata or set the isActive-flag in the user interface. * * @param doi/*w ww. ja va 2 s. c om*/ * @throws DoiClientException */ public void deleteDoiMetadata(String doi) throws DoiClientException { ClientHttpResponse httpResponse = null; HttpDelete deleteMethod = null; try { Log.debug(LOGGER_NAME, " -- URL: " + this.serverUrl + "/metadata"); deleteMethod = new HttpDelete(createUrl("metadata/" + doi)); httpResponse = requestFactory.execute(deleteMethod, new UsernamePasswordCredentials(username, password), AuthScope.ANY); int status = httpResponse.getRawStatusCode(); Log.debug(LOGGER_NAME, " -- Request status code: " + status); // Ignore NOT FOUND (trying to delete a non existing doi metadata) if ((status != HttpStatus.SC_NOT_FOUND) && (status != HttpStatus.SC_OK)) { Log.info(LOGGER_NAME, "Delete DOI metadata end -- Error: " + httpResponse.getStatusText()); throw new DoiClientException(httpResponse.getStatusText()); } else { Log.info(LOGGER_NAME, "DeleteDOI metadata end"); } } catch (Exception ex) { Log.error(LOGGER_NAME, " -- Error (exception): " + ex.getMessage()); throw new DoiClientException(ex.getMessage()); } finally { if (deleteMethod != null) { deleteMethod.releaseConnection(); } // Release the connection. IOUtils.closeQuietly(httpResponse); } }
From source file:org.fao.geonet.doi.client.DoiClient.java
public void deleteDoi(String doi) throws DoiClientException { ClientHttpResponse httpResponse = null; HttpDelete deleteMethod = null;//from www .java 2s . c o m try { Log.debug(LOGGER_NAME, " -- URL: " + this.serverUrl + "/metadata"); deleteMethod = new HttpDelete(createUrl("doi/" + doi)); httpResponse = requestFactory.execute(deleteMethod, new UsernamePasswordCredentials(username, password), AuthScope.ANY); int status = httpResponse.getRawStatusCode(); Log.debug(LOGGER_NAME, " -- Request status code: " + status); // Ignore NOT FOUND (trying to delete a non existing doi metadata) if ((status != HttpStatus.SC_NOT_FOUND) && (status != HttpStatus.SC_OK)) { Log.info(LOGGER_NAME, "Delete DOI end -- Error: " + httpResponse.getStatusText()); throw new DoiClientException(httpResponse.getStatusText()); } else { Log.info(LOGGER_NAME, "DeleteDOI end"); } } catch (Exception ex) { Log.error(LOGGER_NAME, " -- Error (exception): " + ex.getMessage()); throw new DoiClientException(ex.getMessage()); } finally { if (deleteMethod != null) { deleteMethod.releaseConnection(); } // Release the connection. IOUtils.closeQuietly(httpResponse); } }
From source file:org.fao.geonet.doi.client.DoiClient.java
private String retrieve(String url, String entity) throws DoiClientException { ClientHttpResponse httpResponse = null; HttpGet getMethod = null;/* www. j a v a 2 s.co m*/ try { Log.debug(LOGGER_NAME, " -- URL: " + url); getMethod = new HttpGet(url); httpResponse = requestFactory.execute(getMethod, new UsernamePasswordCredentials(username, password), AuthScope.ANY); int status = httpResponse.getRawStatusCode(); Log.debug(LOGGER_NAME, " -- Request status code: " + status); if (status == HttpStatus.SC_OK) { return CharStreams.toString(new InputStreamReader(httpResponse.getBody())); } else if (status == HttpStatus.SC_NO_CONTENT) { return null; // Not found } else if (status == HttpStatus.SC_NOT_FOUND) { return null; // Not found } else { Log.info(LOGGER_NAME, "Retrieve DOI metadata end -- Error: " + httpResponse.getStatusText()); throw new DoiClientException(httpResponse.getStatusText() + CharStreams.toString(new InputStreamReader(httpResponse.getBody()))); } } catch (Exception ex) { Log.error(LOGGER_NAME, " -- Error (exception): " + ex.getMessage()); throw new DoiClientException(ex.getMessage()); } finally { if (getMethod != null) { getMethod.releaseConnection(); } // Release the connection. IOUtils.closeQuietly(httpResponse); } }
From source file:org.geosdi.geoplatform.publish.HttpUtilsLocal.java
/** * Used to query for REST resources./*from w ww .j a va 2 s. c o m*/ * * @param url The URL of the REST resource to query about. * @param username * @param pw * @return true on 200, false on 404. * @throws RuntimeException on unhandled status or exceptions. */ public static boolean exists(String url, String username, String pw) { GetMethod httpMethod = null; try { HttpClient client = new HttpClient(); setAuth(client, url, username, pw); httpMethod = new GetMethod(url); client.getHttpConnectionManager().getParams().setConnectionTimeout(2000); int status = client.executeMethod(httpMethod); switch (status) { case HttpStatus.SC_OK: return true; case HttpStatus.SC_NOT_FOUND: return false; default: throw new RuntimeException("Unhandled response status at '" + url + "': (" + status + ") " + httpMethod.getStatusText()); } } catch (ConnectException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (httpMethod != null) { httpMethod.releaseConnection(); } } }
From source file:org.gots.server.auth.TempTokenAuthenticationServlet.java
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get request parameters String applicationName = req.getParameter(APPLICATION_NAME_PARAM); String deviceId = req.getParameter(DEVICE_ID_PARAM); String deviceDescription = req.getParameter(DEVICE_DESCRIPTION_PARAM); String permission = req.getParameter(PERMISSION_PARAM); String revokeParam = req.getParameter(REVOKE_PARAM); boolean revoke = Boolean.valueOf(revokeParam); // If one of the required parameters is null or empty, send an // error with the 400 status if (!revoke && (StringUtils.isEmpty(applicationName) || StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(permission))) { log.error(// w ww . j a v a2 s. co m "The following request parameters are mandatory to acquire an authentication token: applicationName, deviceId, permission."); resp.sendError(HttpStatus.SC_BAD_REQUEST); return; } if (revoke && (StringUtils.isEmpty(applicationName) || StringUtils.isEmpty(deviceId))) { log.error( "The following request parameters are mandatory to revoke an authentication token: applicationName, deviceId."); resp.sendError(HttpStatus.SC_BAD_REQUEST); return; } // Decode parameters applicationName = URIUtil.decode(applicationName); deviceId = URIUtil.decode(deviceId); if (!StringUtils.isEmpty(deviceDescription)) { deviceDescription = URIUtil.decode(deviceDescription); } if (!StringUtils.isEmpty(permission)) { permission = URIUtil.decode(permission); } // Get user name from request Principal Principal principal = req.getUserPrincipal(); if (principal == null) { resp.sendError(HttpStatus.SC_UNAUTHORIZED); return; } String userName = principal.getName(); log.error("The principal user is " + userName); // Write response String response = null; TokenAuthenticationService tokenAuthService = Framework.getLocalService(TokenAuthenticationService.class); try { // Token acquisition: acquire token and write it to the response // body if (!revoke) { response = tokenAuthService.acquireToken(userName, applicationName, deviceId, deviceDescription, permission); } // Token revocation else { String token = tokenAuthService.getToken(userName, applicationName, deviceId); if (token == null) { response = String.format( "No token found for userName %s, applicationName %s and deviceId %s; nothing to do.", userName, applicationName, deviceId); } else { tokenAuthService.revokeToken(token); response = String.format("Token revoked for userName %s, applicationName %s and deviceId %s.", userName, applicationName, deviceId); } } sendTextResponse(resp, response); } catch (Exception e) { // Should never happen as parameters have already been checked resp.sendError(HttpStatus.SC_NOT_FOUND); } }
From source file:org.hydracache.client.transport.HttpTransport.java
@Override public ResponseMessage sendRequest(RequestMessage requestMessage) throws Exception { if (httpClient == null) throw new IllegalStateException("Establish connection first."); HttpMethod getMethod = getMethod(requestMessage); try {/*from w w w. j a va 2 s. co m*/ int responseCode = httpClient.executeMethod(getMethod); if (responseCode == HttpStatus.SC_NOT_FOUND) return null; ResponseMessageHandler handler = handlers.get(responseCode); return (handler == null ? null : handler.accept(responseCode, getMethod.getResponseBody())); } finally { getMethod.releaseConnection(); } }