For some reason I'm getting null pointer exception. It's downloading the image here and logcat points me to call
public Result call(final String method, final String apiKey, final String... params) {
return call(method, apiKey, map(params));
}
/**
* Performs the web-service call. If the <code>session</code> parameter is
* <code>non-null</code> then an authenticated call is made. If it's
* <code>null</code> then an unauthenticated call is made.<br/>
* The <code>apiKey</code> parameter is always required, even when a valid
* session is passed to this method.
*
* #param method The method to call
* #param apiKey A Last.fm API key
* #param params Parameters
* #param session A Session instance or <code>null</code>
* #return the result of the operation
*/
public Result call(final String method, final String apiKey, Map<String, String> params) {
params = new WeakHashMap<String, String>(params);
InputStream inputStream = null;
// no entry in cache, load from web
if (inputStream == null) {
// fill parameter map with apiKey and session info
params.put(PARAM_API_KEY, apiKey);
try {
final HttpURLConnection urlConnection = openPostConnection(method, params);
inputStream = getInputStreamFromConnection(urlConnection);
if (inputStream == null) {
lastResult = Result.createHttpErrorResult(urlConnection.getResponseCode(),
urlConnection.getResponseMessage());
return lastResult;
}
} catch (final IOException ignored) {
}
}
try {
final Result result = createResultFromInputStream(inputStream);
lastResult = result;
return result;
} catch (final IOException ignored) {
} catch (final SAXException ignored) {
}
return null;
}
It finally cracks at the line "new InputSource(new InputStreamReader(inputStream, "UTF-8")));".
/**
* #param inputStream
* #return
* #throws SAXException
* #throws IOException
*/
private Result createResultFromInputStream(final InputStream inputStream) throws SAXException,
IOException {
final Document document = newDocumentBuilder().parse(
new InputSource(new InputStreamReader(inputStream, "UTF-8")));
final Element root = document.getDocumentElement(); // lfm element
final String statusString = root.getAttribute("status");
final Status status = "ok".equals(statusString) ? Status.OK : Status.FAILED;
if (status == Status.FAILED) {
final Element errorElement = (Element)root.getElementsByTagName("error").item(0);
final int errorCode = Integer.parseInt(errorElement.getAttribute("code"));
final String message = errorElement.getTextContent();
return Result.createRestErrorResult(errorCode, message);
} else {
return Result.createOkResult(document);
}
}
Any ideas? I have no idea what might be wrong. If sufficient info is provided then let me know - I'll get what you need. I'm a beginner. :)
Related
I have an apache wicket panel for google recaptcha where I integrate wicket with google recaptcha and everything is working as it should, but I need to add an error message or feedbackpanel I am not quite good with wicket, so please advise me what is the best solution for my case?
public class ReCaptchaPanel extends Panel {
private static final long serialVersionUID = -8346261172610929107L;
private static final Logger log = LoggerFactory.getLogger(ReCaptchaPanel.class);
final String publicKey = "6LdrIgceAAAAADWF-gIz_OHJg_5gLu7IVw11hTnt";
final String secretKey = "6LdrIgceAAAAAE9qxHViBj3Sl8uyqUVdluugPRTq";
final String scriptClassName = "data-sitekey";
final String recCaptchaClassName = "g-recaptcha";
String currentLanguage = getLocale().getLanguage();
String scriptValue = "https://www.google.com/recaptcha/api.js?hl=" + currentLanguage;
/* in the following link you can find the language codes
* https://developers.google.com/recaptcha/docs/language
* */
public ReCaptchaPanel(String id) {
super(id);
final IFeedbackMessageFilter feedbackFilter = new ContainerFeedbackMessageFilter(this);
final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback", feedbackFilter);
feedbackPanel.setOutputMarkupId(true);
add(feedbackPanel);
add(new Label("reCaptchaComponent", "").add(new SimpleAttributeModifier("class", recCaptchaClassName))
.add(new SimpleAttributeModifier(scriptClassName, publicKey)));
add(new Label("reCaptchaScript", "").add(new SimpleAttributeModifier("src", scriptValue)));
}
public ReCaptchaPanel(String id, IModel model) {
super(id, model);
}
/**
* Validates Google reCAPTCHA V2 .
*
* #param secretKey Secret key (key given for communication between arena and Google)
* #param response reCAPTCHA response from client side (g-recaptcha-response).
* #return true if validation successful, false otherwise.
*/
public synchronized boolean isCaptchaValid(String secretKey, String response) {
try {
String url = "https://www.google.com/recaptcha/api/siteverify",
params = "secret=" + secretKey + "&response=" + response;
HttpURLConnection http = (HttpURLConnection) new URL(url).openConnection();
http.setDoOutput(true);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
OutputStream out = http.getOutputStream();
out.write(params.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
InputStream res = http.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(res, StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
JSONObject json = new JSONObject(sb.toString());
res.close();
log.info("Google ReCaptcha has been verified");
return json.getBoolean("success");
} catch (Exception e) {
e.printStackTrace();
}
log.error(" Google ReCaptcha failed");
return false;
}
public synchronized boolean isCaptchaValid() {
HttpServletRequest httpServletRequest = ApplicationBase.getHttpServletRequest();
assert httpServletRequest != null;
String response = httpServletRequest.getParameter("g-recaptcha-
response");
return isCaptchaValid(secretKey, response);
}
}
I use this panel in another wicket component ( form) ,
class FormEmail extends Form {
.........
final ReCaptchaPanel reCaptchaPanel = new ReCaptchaPanel("reCaptchaPanel") {
/**
* #see org.apache.wicket.Component#isVisible()
*/
#Override
public boolean isVisible() {
SessionBase session = (SessionBase) getSession();
ItemListPanelConfigParams configParams = new ItemListPanelConfigParams(session);
return configParams.isShowCaptcha();
}
};
add(reCaptchaPanel);
Button buttonSend = new Button("buttonSend") {
#Override
public void onSubmit() {
onSendCallback(emailRecipient, emailReplyTo, comment);
setResponsePage(callbackPage);
}
};
what i need to achieve is that add a message when the verify method in code above is false; I mean an error message, anyone knows how.
I'm trying to develop a XSS Filter. All works fine, but the wrapper is losing the multipart fields.
After the filter, in the controller, when I try to obtain the value of a multipart field always is empty.
I have the following wrapper:
public class XSSRequestWrapperMultipart extends HttpServletRequestWrapper {
/** Constructor. */
public XSSRequestWrapperMultipart(HttpServletRequest aRequest) throws IOException {
super(aRequest);
ServletFileUpload upload = new ServletFileUpload( new DiskFileItemFactory());
try {
List<FileItem> fileItems = upload.parseRequest(aRequest);
convertToMaps(fileItems);
}
catch(FileUploadException ex){
throw new IOException("Cannot parse underlying request: " + ex.toString());
}
}
/**
* Return all request parameter names, for both regular controls and file upload
* controls.
*/
#Override public Enumeration<String> getParameterNames() {
Set<String> allNames = new LinkedHashSet<>();
allNames.addAll(fRegularParams.keySet());
allNames.addAll(fFileParams.keySet());
return Collections.enumeration(allNames);
}
/**
* Return the parameter value. Applies only to regular parameters, not to
* file upload parameters.
*/
#Override public String getParameter(String aName) {
String result = null;
List<String> values = fRegularParams.get(aName);
if(values == null){
//you might try the wrappee, to see if it has a value
}
else if (values.isEmpty()) {
//param name known, but no values present
result = "";
}
else {
//return first value in list
result = values.get(FIRST_VALUE);
}
return result;
}
/**
* Return the parameter values. Applies only to regular parameters,
* not to file upload parameters.
*/
#Override public String[] getParameterValues(String aName) {
String[] result = null;
List<String> values = fRegularParams.get(aName);
if(values != null) {
result = values.toArray(new String[values.size()]);
}
return result;
}
/**
* Return a {#code Map<String, List<String>>} for all regular parameters.
* Does not return any file upload parameters at all.
*/
#Override public Map<String, List<String>> getParameterMap() {
return Collections.unmodifiableMap(fRegularParams);
}
/**
* Return a {#code List<FileItem>}, in the same order as they appear
* in the underlying request.
*/
public List<FileItem> getFileItems(){
return new ArrayList<FileItem>(fFileParams.values());
}
/**
* Return the {#link FileItem} of the given name.
* <P>If the name is unknown, then return <tt>null</tt>.
*/
public FileItem getFileItem(String aFieldName){
return fFileParams.get(aFieldName);
}
// PRIVATE
/** Store regular params only. May be multivalued (hence the List). */
private final Map<String, List<String>> fRegularParams = new LinkedHashMap<>();
/** Store file params only. */
private final Map<String, FileItem> fFileParams = new LinkedHashMap<>();
private static final int FIRST_VALUE = 0;
private void convertToMaps(List<FileItem> aFileItems){
for(FileItem item: aFileItems) {
if ( isFileUploadField(item) ) {
fFileParams.put(item.getFieldName(), item);
}
else {
if( alreadyHasValue(item) ){
addMultivaluedItem(item);
}
else {
addSingleValueItem(item);
}
}
}
}
private boolean isFileUploadField(FileItem aFileItem){
return ! aFileItem.isFormField();
}
private boolean alreadyHasValue(FileItem aItem){
return fRegularParams.get(aItem.getFieldName()) != null;
}
private void addSingleValueItem(FileItem aItem){
List<String> list = new ArrayList<>();
list.add(aItem.getString());
fRegularParams.put(aItem.getFieldName(), list);
}
private void addMultivaluedItem(FileItem aItem){
List<String> values = fRegularParams.get(aItem.getFieldName());
values.add(aItem.getString());
}
}
All relative to the fRegularParams works fine, but the fFileParams always appears as null in the controller.
What can I do to keep the values?
Regards
If you see for fRegularParams, you are touching the getParameter() method of servlet lifecycle.
Here the parameters are already initialized in this phase.
For fFileParams you are trying to get params in constructor, which is too early considering lifecycle of servlet and filters.
Image for your reference:
Solution:
Can you try with method request.getParts() instead?
You can try this approach and override
https://github.com/spring-projects/spring-framework/blob/2f20d6322b7a0fcbbaa80280849e7c31fc78d4a9/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java#L131
Which internally deals with request parts.
https://github.com/spring-projects/spring-framework/blob/2f20d6322b7a0fcbbaa80280849e7c31fc78d4a9/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java#L91
Here "might" be your problem:
private boolean isFileUploadField(FileItem aFileItem){
return ! aFileItem.isFormField();
}
It's returning the opposite response of the boolean value (check the !). Remove it and test if that works.
Only if the FileItem.isFormField() that you can get the form parameter data.
private boolean isFileUploadField(FileItem aFileItem){
return aFileItem.isFormField();
}
Overview:
I am going to use RestTemplate to invoke a get request from external REST webservice.
My code is as follows:
#Slf4j
#Component("AccMemberDetailsApiControllerImpl")
public class AccMemberDetailsApiControllerImpl implements MemberDetailsApiController {
private static final String CONTENT_TYPE_HEADER_NAME = "Content-Type";
private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
private static final String USERID_PARAMETER_NAME = "userId";
private static final String VEHICLEID_PARAMETER_NAME = "vehicleId";
private static final ObjectMapper mapper = new ObjectMapper();
/**
* This constant is used to check whether or not the response from ACC is an empty JSON string
*/
private static final String EMPTY_RESPONSE = "{}";
#Value("${com.blss.memberServices.provider.posServiceURL}")
private String accPosServiceURL;
#Autowired
private RestTemplate restTemplate;
#Autowired
private AccTokenUtility accTokenUtility;
#Autowired
private ResourceMessage resourceMessage;
void setAccTokenUtility(AccTokenUtility accTokenUtility) {
this.accTokenUtility = accTokenUtility;
}
void setResourceMessage(ResourceMessage resourceMessage) {
this.resourceMessage = resourceMessage;
}
/**
* #see MemberDetailsApiController#getMemberDetails(String, String)
*/
#Override
public MemberDetailsModel getMemberDetails(String storeId, String membershipIdentifier) {
/**
* Getting CAD token
*/
String token = accTokenUtility.getCadToken();
/**
* Preparing the request
*/
HttpHeaders headers = new HttpHeaders();
// headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set(CONTENT_TYPE_HEADER_NAME, MediaType.APPLICATION_JSON_VALUE);
headers.set(AUTHORIZATION_HEADER_NAME, token);
HttpEntity<String> entity = new HttpEntity<>(headers);
/**
* Creating the dispatch URL by means of userId and vehicleId
*/
String dispatchURL = accPosServiceURL + "DispatchedEvent/{userId}/{vehicleId}";
/**
* Creating the URL variables and being valued by corresponding method parameters
*/
Map<String, String> parameters = new HashMap<>();
// parameters.put(USERID_PARAMETER_NAME, storeId);
parameters.put(USERID_PARAMETER_NAME, "mr2");
// parameters.put(VEHICLEID_PARAMETER_NAME, membershipIdentifier);
parameters.put(VEHICLEID_PARAMETER_NAME, "VEH1");
/**
* Calling the rest webservice and returning response with body of type {#link AccMemberDetails}
*/
ResponseEntity<String> response;
MemberDetailsModel memberDetailsModel = null;
AccMemberDetails accMemberDetails;
try {
response = restTemplate.exchange(dispatchURL, HttpMethod.GET, entity, String.class, parameters);
if (response == null || StringUtils.isBlank(response.getBody()) || EMPTY_RESPONSE.equals(response.getBody())) {
throw new ResourceNotFoundException(resourceMessage.getMessage(MEMBER_ERROR_NOT_FOUND, storeId, membershipIdentifier));
} else {
accMemberDetails = deserialize(response.getBody(), AccMemberDetails.class);
String accErrorMessage = accMemberDetails.getUserMessage();
if (!StringUtils.isBlank(accErrorMessage)) {
throw new InternalServerException(resourceMessage.getMessage(MEMBER_ERROR_MESSAGE_FROM_API, "ACC", accErrorMessage));
}
memberDetailsModel = convert(accMemberDetails);
}
} catch (RestClientException e) {
handleExceptions(e, storeId, membershipIdentifier);
}
return memberDetailsModel;
}
/**
* This method is responsible for deserializing string REST response into an object of type {#link AccMemberDetails}
*/
<T> T deserialize(final String response, final Class<T> responseClass) {
try {
return mapper.readValue(response, responseClass);
} catch (IOException e) {
throw new InternalServerException(resourceMessage.getMessage(MEMBER_ERROR_MAP_RESPONSE_OBJECT), e);
}
}
/**
* This method is responsible for converting an instance of type {#link AccMemberDetails} to an instance of type
* {#link MemberDetailsModel}
*
* #param accMemberDetails an instance of type {#link AccMemberDetails}
* #return an instance of type {#link MemberDetailsModel}
*/
MemberDetailsModel convert(AccMemberDetails accMemberDetails) {
MemberDetailsModel memberDetailsModel = new MemberDetailsModel();
memberDetailsModel.setEventId(accMemberDetails.getEventId());
memberDetailsModel.setMemberName(accMemberDetails.getMemberName());
memberDetailsModel.setMembershipNumber(accMemberDetails.getMembershipNumber());
memberDetailsModel.setMembershipLevel(accMemberDetails.getPricingLevel());
return memberDetailsModel;
}
/**
* This method is responsible for handling Exceptions may be thrown by ACC REST webservice
*
* #param e an instance of type {#link RestClientException}
* #param storeId an instance of type {#link String} and used in building exception messages
* #param membershipIdentifier an instance of type {#link String} and used in building exception messages
*/
private void handleExceptions(RestClientException e, String storeId, String membershipIdentifier) {
if (e instanceof HttpStatusCodeException) {
HttpStatusCodeException httpStatusCodeException = (HttpStatusCodeException) e;
HttpStatus httpStatusCode = httpStatusCodeException.getStatusCode();
if (404 == httpStatusCode.value()) {
throw new ResourceNotFoundException(resourceMessage.getMessage(MEMBER_ERROR_NOT_FOUND, storeId, membershipIdentifier), e);
} else if (500 == httpStatusCode.value()) {
throw new InternalServerException(resourceMessage.getMessage(MEMBER_SERVER_ERROR, "ACC"), e);
} else {
throw new InternalServerException(resourceMessage.getMessage(MEMBER_HTTP_STATUS_CODE_ERROR, "HttpStatusCodeException", "ACC"), e);
}
} else {
throw new InternalServerException(resourceMessage.getMessage(MEMBER_REST_CLIENT_ERROR, "RestClientException", "ACC"), e);
}
}
Problem
However I got UnhandledHttpStatusException after calling "restTemplate.exchange(dispatchURL, HttpMethod.GET, entity, String.class, parameters);" in the code snippet. the exception stack trace is as follows:
Caused by: org.springframework.web.client.UnknownHttpStatusCodeException: Unknown status code [443] null
at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:60)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:50)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:629)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:565)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:484)
at com.blss.retailServices.memberServices.controllers.impl.acc.AccMemberDetailsApiControllerImpl.getMemberDetails(AccMemberDetailsApiControllerImpl.java:110)
Now I would be grateful if anyone could suggest me a solution.
I called this webservice with curl by using "-v" in order to get more info in response. As a result, I got the same exception (443) from their side. So, It sounds like they should have a better exception handler to return meaningful exception messages.
We are using apache commons mail, specifically the ImageHtmlEmail. We really would like to log every email sent - exactly as it will be sent - in the perfect world it would be something you could paste into sendmail - with all headers and other information included.
This is primarily to troubleshoot some problems we've been having with it turning up as text/plain rather than text/html - but also because it would be nice to have a record of exactly what the system sent out stored in our logs.
So essentially - the dream is a function that would take an ImageHtmlEmail and return a string - as it will be sent. I know I could render it into a string myself, but then I'm bypassing whatever is being done in the library function, which is what we really want to capture. I tried BuildMimeMessage and then getMimeMessage, which I think is probably the correct first step - but that just leaves me with the question of how to turn a mimemessage into a string.
I have a sort of solution - but would love a better one:
/**
* add content of this type
*
* #param builder
* #param content
*/
private static void addContent(final StringBuilder builder, final Object content)
{
try
{
if (content instanceof MimeMultipart)
{
final MimeMultipart multi = (MimeMultipart) content;
for (int i = 0; i < multi.getCount(); i++)
{
addContent(builder, ((MimeMultipart) content).getBodyPart(i));
}
}
else if (content instanceof MimeBodyPart)
{
final MimeBodyPart message = (MimeBodyPart) content;
final Enumeration<?> headers = message.getAllHeaderLines();
while (headers.hasMoreElements())
{
final String line = (String) headers.nextElement();
builder.append(line).append("\n");
}
addContent(builder, message.getContent());
}
else if (content instanceof String)
{
builder.append((String) content).append("\n");
}
else
{
System.out.println(content.getClass().getName());
throw CommonException.notImplementedYet();
}
}
catch (final Exception theException)
{
throw CommonException.insteadOf(theException);
}
}
/**
* get a string from an email
*
* #param email
* #return
*/
public static String fromHtmlEmail(final ImageHtmlEmail email)
{
return fromMimeMessage(email.getMimeMessage());
}
/**
* #param message
* #return a string from a mime message
*/
private static String fromMimeMessage(final MimeMessage message)
{
try
{
message.saveChanges();
final StringBuilder output = new StringBuilder();
final Enumeration<?> headers = message.getAllHeaderLines();
while (headers.hasMoreElements())
{
final String line = (String) headers.nextElement();
output.append(line).append("\n");
}
addContent(output, message.getContent());
return output.toString();
}
catch (final Exception theException)
{
throw CommonException.insteadOf(theException);
}
}
}
java.net.SocketException: Unexpected end of file from server
The client sends a query to the server by using an URL. I have a HTTPServer that parses info from the URL. Using that info, the server does some work and then returns the response to the client.
Let's say the URL is:
http://localhost:9090/find?term=healthy&term=cooking&count=3
This works fine. But when count is greater or equal to 4, I get java.net.SocketException.
To debug the error, I print out the URL. System.out.println(input);
When count>=4, the URL gets printed two times in the console. Please help.
private final HttpServer server;
public Server(int port){
server = HttpServer.create(new InetSocketAddress(port), MAX_BACKLOG);
server.createContext("/isbn", new ISBNHandler());
server.createContext("/find", new TitleHandler());
}
static class TitleHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
int count=5;
String input=t.getRequestURI().toASCIIString();//get the URL
System.out.println(input);
//using info from URL to do some work
String response = builder.toString();//StringBuilder
// System.out.println(response);
t.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
Exception
java.net.SocketException: Unexpected end of file from server at
sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source) at
sun.net.www.http.HttpClient.parseHTTP(Unknown Source) at
sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source) at
sun.net.www.http.HttpClient.parseHTTP(Unknown Source) at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown
Source) at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at TestHarness.assertJSONResponse(TestHarness.java:101) at
TestHarness.testServer(TestHarness.java:38) at
TestHarness.main(TestHarness.java:25)
TestHarness
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Sample main method with initial acceptance tests to help you along
*/
public class TestHarness {
private static Pattern MAP_PATTERN = Pattern.compile("(['\"])(.*?)\\1:\\s*(['\"])(.*?)\\3");
private static Pattern LIST_PATTERN = Pattern.compile("\\{(.*?)\\}");
public static void main(String[] args) throws IOException {
/*
if (args.length < 1) {
System.out.println("The test harness requires a single parameter: the location of the CSV file to parse");
}
*/
BookServer server = new BookServer(9090, new File("books.csv"));
server.start();
testServer();
server.stop();
}
/**
* Run initial acceptance tests
*/
#SuppressWarnings("unchecked")
private static void testServer() {
assertJSONResponse("Book Test", "http://localhost:9090/isbn/9780470052327",
createMap("isbn", "9780470052327", "title", "Techniques of Healthy Cooking", "author", "Mary Dierdre Donovan", "publisher", "Wiley", "publishedYear", "2007"));
assertJSONResponse("Book Test", "http://localhost:9090/isbn/9780451169525",
createMap("isbn", "9780451169525", "title", "Misery", "author", "Stephen King", "publisher", "Signet", "publishedYear", "1987"));
assertJSONResponse("Query Test", "http://localhost:9090/find?term=healthy&term=cooking&count=4",
Arrays.asList(createMap("isbn", "9780470052327", "title", "Techniques of Healthy Cooking", "author", "Mary Dierdre Donovan", "publisher", "Wiley", "publishedYear", "2007")));
}
/**
* Helper method to convert the vararg parameters into a Map. Assumes alternating key, value, key, value... and calls
* toString on all args
*
* #param args the parameters to put in the map, alternating key ancd value
* #return Map of String representations of the parameters
*/
private static Map<String, String> createMap(Object ... args) {
Map<String, String> map = new HashMap<String, String>();
for (int i=0; i < args.length; i+=2) {
map.put(args[i].toString(), args[i+1].toString());
}
return map;
}
/**
* Parses a JSON list of maps
* NOTE: assumes all keys and values in the nested maps are quoted
*
* #param content the JSON representation
* #return a list of parsed Map content
*/
private static List<Map<String, String>> parseJSONList(CharSequence content) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Matcher m = LIST_PATTERN.matcher(content);
while(m.find()) {
list.add(parseJSONMap(m.group(1)));
}
return list;
}
/**
* Parse JSON encoded content into a Java Map.
* NOTE: Assumes that all elements in the map are quoted
*
* #param content the JSON representation to be parsed
* #return A map of parsed content
*/
private static Map<String, String> parseJSONMap(CharSequence content) {
Map<String, String> map = new HashMap<String, String>();
Matcher m = MAP_PATTERN.matcher(content);
while (m.find()) {
map.put(m.group(2), m.group(4));
}
return map;
}
/**
* Retrieve content from a test URL and assert that its content is the expected. Results will be printed to System.out for convenience
*
* #param testName Name of the test, to be used simply for labelling
* #param urlString The URL to test
* #param expected The content expected at that URL
*/
private static void assertJSONResponse(String testName, String urlString, Object expected) {
try {
URL url = new URL(urlString);
HttpURLConnection con = ((HttpURLConnection)url.openConnection());
if (!assertTest(testName + " - response code", con.getResponseCode(), 200)) return;
StringBuilder b = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while((line = r.readLine()) != null) b.append(line);
String result = b.toString();
assertTest(testName + " - content retrieved", !result.isEmpty(), true);
Object parsed = result.trim().startsWith("[") ? parseJSONList(result) : parseJSONMap(result);
assertTest(testName + " - parsed content match", parsed, expected);
} catch (Exception e) {
System.out.println(testName + ": <<<FAILED with Exception>>>");
e.printStackTrace(System.out);
}
}
/**
* Log the results of a test assertion
*
* #param testName Name of the test, to be used simply for labelling
* #param result The result of the operation under test
* #param expected The expected content that the result will be compared against
* #return whether the test was successful
*/
private static boolean assertTest(String testName, Object result, Object expected) {
boolean passed = result.equals(expected);
System.out.println(testName + (passed ? ": <<<PASSED>>>" : String.format(": <<<FAILED>>> expected '%s' but was '%s'", expected, result)));
return passed;
}
}
I fixed the error. The server calls a private sorting function, but I made a bug in the sorting function. The server crashed.