List of usage examples for java.net HttpURLConnection HTTP_PAYMENT_REQUIRED
int HTTP_PAYMENT_REQUIRED
To view the source code for java.net HttpURLConnection HTTP_PAYMENT_REQUIRED.
Click Source Link
From source file:Main.java
public static void main(String[] argv) throws Exception { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection(); con.setRequestMethod("HEAD"); System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_PAYMENT_REQUIRED); }
From source file:com.leafhut.open_source.VerifyReceipt.java
@Override public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) { // Set up logger LoggerService logger = serviceProvider.getLoggerService(VerifyReceipt.class); String startMessage = "Processing receipt..."; logger.info(startMessage);/*from w w w . j av a 2 s . c o m*/ HttpService http; try { http = serviceProvider.getHttpService(); } catch (ServiceNotActivatedException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); logger.error(exceptionLogMessage); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } if (http == null) { String failureReason = "HTTP Service is null."; logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } // Fetch parameters sent via POST String encodedReceipt = null; try { JSONObject jsonObj = new JSONObject(request.getBody()); if (!jsonObj.isNull(kReceiptParameter)) { encodedReceipt = jsonObj.getString(kReceiptParameter); } } catch (JSONException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); String failureReason = "Invalid or missing parameter."; logger.error(exceptionLogMessage); logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } // Create JSON representation of receipt JSONObject validationBodyJSON = new JSONObject(); try { validationBodyJSON.put(kReceiptValidationKey, encodedReceipt); } catch (JSONException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); String failureReason = "Could not create JSON for receipt validation server."; logger.error(exceptionLogMessage); logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } String validationBodyString = validationBodyJSON.toString(); // create the HTTP request PostRequest req; try { req = new PostRequest(validationServerURL, validationBodyString); } catch (MalformedURLException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); String failureReason = "Invalid URL for receipt validation server."; logger.error(exceptionLogMessage); logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); errorMap.put(kFailingURLStringKey, validationServerURL); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } // Send the request. This method call will not return until the server returns. // note that this method may throw AccessDeniedException if the URL is whitelisted or rate limited, // or TimeoutException if the server took too long to return HttpResponse response; try { response = http.post(req); } catch (AccessDeniedException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); String failureReason = "HTTP request refused by StackMob custom code environment."; String suggestionMessage = "Check rate limiting, whitelisting, and blacklisting in the StackMob custom code environment."; logger.error(exceptionLogMessage); logger.error(failureReason); logger.debug(suggestionMessage); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_FORBIDDEN); errorMap.put(kErrorDescriptionKey, kErrorCode403Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); errorMap.put(kFailureReasonKey, failureReason); errorMap.put(kRecoverySuggestionKey, suggestionMessage); return new ResponseToProcess(HttpURLConnection.HTTP_FORBIDDEN, errorMap); } catch (TimeoutException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); String failureReason = "HTTP request to receipt validation server timed out."; logger.error(exceptionLogMessage); logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_GATEWAY_TIMEOUT); errorMap.put(kErrorDescriptionKey, kErrorCode504Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_GATEWAY_TIMEOUT, errorMap); } if (response == null) { String failureReason = "Response from receipt validation server is null."; logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } // Parse the response from the server JSONObject serverResponseJSON; try { serverResponseJSON = new JSONObject(response.getBody()); } catch (JSONException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); String failureReason = "Could not parse JSON response from receipt validation server."; logger.error(exceptionLogMessage); logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } int validationStatus = -1; try { if (!serverResponseJSON.isNull(kValidationResponseStatusKey)) { validationStatus = serverResponseJSON.getInt(kValidationResponseStatusKey); } } catch (JSONException e) { String exceptionLogMessage = e.getClass().getName() + ": " + e.getMessage(); String failureReason = "Missing or invalid status code from receipt validation server."; logger.error(exceptionLogMessage); logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_INTERNAL_ERROR); errorMap.put(kErrorDescriptionKey, kErrorCode500Description); errorMap.put(kExceptionNameKey, e.getClass().getName()); errorMap.put(kExceptionMessageKey, e.getMessage()); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_INTERNAL_ERROR, errorMap); } // Take action based on receipt validation if (validationStatus == 0) { // // Receipt is valid // // This is where you could take any server-side actions that were required to fulfill the purchase. // See the StackMob custom code documentation for more details: // https://developer.preview.stackmob.com/tutorials/custom%20code // } else { // // Receipt is invalid // String failureReason = "Invalid receipt."; logger.error(failureReason); Map<String, Object> errorMap = new HashMap<String, Object>(); errorMap.put(kErrorCodeKey, HttpURLConnection.HTTP_PAYMENT_REQUIRED); errorMap.put(kErrorDescriptionKey, kErrorCode402Description); errorMap.put(kFailureReasonKey, failureReason); return new ResponseToProcess(HttpURLConnection.HTTP_PAYMENT_REQUIRED, errorMap); } // Send human-readable server response to calling client // Note: The parsing below is brittle (depends on there never being more than two layers of JSON). // This probably should be generalized using recursion. Map<String, Object> returnMap = new HashMap<String, Object>(); Iterator<?> keys = serverResponseJSON.keys(); while (keys.hasNext()) { String key = (String) keys.next(); try { if (serverResponseJSON.get(key) instanceof JSONObject) { JSONObject nestedJSON = (JSONObject) serverResponseJSON.get(key); Iterator<?> nestedKeys = nestedJSON.keys(); while (nestedKeys.hasNext()) { String nestedKey = (String) nestedKeys.next(); Object nestedValue = nestedJSON.get(nestedKey); returnMap.put(nestedKey, nestedValue.toString()); } } else { Object value = serverResponseJSON.get(key); returnMap.put(key, value.toString()); } } catch (JSONException e) { logger.debug(e.getMessage()); e.printStackTrace(); } } String finishMessage = "Receipt is valid."; logger.info(finishMessage); return new ResponseToProcess(HttpURLConnection.HTTP_OK, returnMap); }