JAX-RS response - remove invalid XML characters before read entity - java

Is there any possibility to remove invalid characters from XML response before trying to read entity?
Response searchResponse = webTarget.request(MediaType.APPLICATION_XML).get();
/..
SearchOutput searchOutput = searchResponse.readEntity(SearchOutput.class);
This code works fine when XML is correct but fails when response contains invalid Unicode characters.
How to workaround that assuming I can't change these invalid data in a database?

Related

Prettify GraphQL query string in Java

I have a Java program which, among other things, reads raw GraphQL queries from XML files and sends them to a given endpoint via HTTP. The problem is that server accepts only proper formatted GraphQL queries, which means no extra spaces, newlines etc. are allowed. So basically I have to write them in a single line, because any newline symbol will break whole query by adding lots of spaces to match XML hierarchy (element containing query is not root element).
As you can tell, single-lined queries, especially long ones are not human-friendly and they're hard to read. There are a lot of code formatters/prettifyers here and there, online, inside IDEA, Postman, Insomnia etc, all of them can do it in a single button click.
Working XML file (one-lined query):
<request>
<url>http://localhost:8080/graphql</url>
<type>GRAPHQL</type>
<body>mutation { login(input: {username: \"user\", password: \"12345\"}) {status}}</body>
</request>
Desired XML file (multi-line query):
<request>
<url>http://localhost:8080/graphql</url>
<type>GRAPHQL</type>
<body>
mutation {
login(input: {username: "user", password: "12345"}) {
status
}
}
</body>
</request>
How can I deal with that scenario? Before sending, I should perform formatting on the 'body' string. Is there any 'prettification' library, or should I write custom symbols 'remover'?
graphql-java already come with AstPrinter that can pretty print a GraphQL AST node. So you can first convert the query string to the AST node and then use it to prettify the query string:
String gql = "mutation { login(input: {username: \"user\", password: \"12345\"}) {status}}";
Parser parser = new Parser();
Document doc = parser.parseDocument(gql);
System.out.println(AstPrinter.printAst(doc)); //which should print out a prettified query here

Converting from HttpURLConnection TO Retrofit2 Issues

I'm upgrading some legacy Android / Java code that pulls in data from a server. The legacy code uses HttpURLConnection and pulls in a inputStream and converts it to a GZipInputStream. The result is then converted to a JSONObject. Then the data is parsed manually. This all works fine, meaning valid json is returned as it starts with '{'
I'm trying to updgrade this code using Retrofit2 and the GsonConverterFactory so I can auto map the json to Classes.
I'm getting an error: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
This tells me that the data is coming in as a string and not a JSON object.
NOTE: I'm sending the same headers and query parameters for both my legacy code and new Retrofit implementation --- but different result -- can't figure out why.
The issue was that the server was expecting all of my query parameters to be in one field and the field named "Object" and in a json format i.e: Object={"field1":"fieldOneValue","field2":"fieldTwoValue"} AND had to be Url Uncoded Format:
#FormUrlEncoded
#POST("testing/myapi.api")
Observable<MyClass> getMyApiData(#Url String url, #Field("Object") String object);

How do I get the value of a key if it contains a space in Rest Assured / Serenity?

I am trying to use Rest Assured in the Serenity framework to validate an endpoint response. I send an xml body to the endpoint and expect a JSON response back like so:
{"Entry ID" : "654123"}
I want to send the XML and verify in the JSON response that the value of the key "Entry ID" is not empty or null. The problem is, the key has a space in it, and I believe it is causing an error. Here is what I have so far:
SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);
This produces the error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class Entry
# line 1, column 33.
Entry ID
^
1 error
I have tried wrapping the "Entry ID" term in different ways to no avail:
.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))
Is it possible to get the value of a key that contains a space in Rest Assured?
You just need to escape the key with single quotes:
then().body("'Entry ID'", not(isEmptyOrNullString()))
Here's an example (tested in version 3.0.6):
// Given
String json = "{\"Entry ID\" : \"654123\"}";
// When
JsonPath jsonPath = JsonPath.from(json);
// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));

xmlhttp.setRequestHeader not working

this is my code
// assume var data has japanese characters
xmlhttp.open("POST","adminUpdate?&value="+data,true); // tried GET as well
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xmlhttp.send();
if I insert alert(data) then i can see japanese characters perfectly fine.
But on the server side (servlet class) when I add this code :
String query = request.getParameter("value");
system.out.println(query)
Now I see garbage value ??????
Ok so I added this line server side :
System.out.println("content type : "+ request.getContentType());
and I got this : text/plain;charset=UTF-8
So now my question is if the encoding is set correctly then why I cant see Japanese characters
One option is to send the query parameters as part of the request body and have the content type set to application/x-www-form-urlencoded.
Then, before getting the parameter, set the request's content character encoding
request.setCharacterEncoding("UTF-8");
String query = request.getParameter("value");
Note that wherever you're printing the query value has to be able to display UTF-8 encoded characters.

How to decode a charcter encoding

My application is posting a request through http and gets a response as a String. The string has delimited character of records. When I try to split the records with the delimiter, it is not able to identify as the response string delimiter character is encoded in different format. Is there a way I can convert the response encoded character or identify the encoding type of my response ?

Categories