I want to communicate using JSON on the wicket page! By the way, with an encrypted request ...
Set the public key to pubKey.
myjsencrypt.js
my.jsencrypt = {
pubKey: '-----BEGIN PUBLIC KEY-----\
Public key content
-----END PUBLIC KEY-----\
'
Initialize.
myjsencrypt.js
// encryption setting
var encrypt = new JSEncrypt();
encrypt.setPublicKey(my.jsencrypt.pubKey);
Throw a request.
myjsencrypt.js
// request
var xhr = new XMLHttpRequest();
xhr.open("POST",Destination URL, true);
xhr.setRequestHeader("content-type", "application/json");
xhr.send( encrypt.encrypt(JSON.stringify(data)) );
There seem to be various things, but jackson seemed to be good, so I tried using it. It's actually easy to use. Just set the installation in pom.xml.
pom.xml
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.2</version>
</dependency>
Get the request and make it a parameter. For RSAUtil, refer to RSA Encryption / Decryption with java 8.
MyJsonPage.java
RequestCycle requestCycle = getRequestCycle();
final WebRequest webRequest = (WebRequest) getRequest();
final HttpServletRequest rawRequest = (HttpServletRequest) webRequest.getContainerRequest();
BufferedReader br = null;
JsonFactory jsonFactory = new JsonFactory();
ObjectMapper jsonMapper = new ObjectMapper(jsonFactory);
// <Parameter name,value>Make a Map of
TypeReference<ConcurrentHashMap<String,String>> typeRef = new TypeReference<ConcurrentHashMap<String,String>>(){};
try {
StringBuffer sb = new StringBuffer();
br = rawRequest.getReader();
for (String line = br.readLine(); line != null; line = br.readLine()) {
sb.append(line);
}
// decrypt RSA input
PrivateKey privKey = RSAUtil.readPrvateKeyFromPem(Private key file);
String decrypted = RSAUtil.decryptWithPrivateKey(sb.toString(), privKey);
// decrypted params to json map
inputJsonMap = jsonMapper.readValue(decrypted, typeRef);
} catch (Exception e) {
/*Error handling*/
}
Now that the parameters have been obtained, process on this page ...
Prepare a method for setting Content-Type.
MyJsonPage.java
// Set content-type
protected String getContentType() {
final String encoding = getApplication().getRequestCycleSettings().getResponseRequestEncoding();
return "application/json; charset=" + encoding;
}
Returns JSON for the result.
MyJsonPage.java
// build JSON response
try {
jsonResponse = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(returnObject);
} catch (JsonProcessingException e) {
/*Error handling*/
}
requestCycle.scheduleRequestHandlerAfterCurrent(
new ResourceStreamRequestHandler(
new StringResourceStream(jsonResponse) ) {
@Override
public void respond(IRequestCycle requestCycle) {
((WebResponse) requestCycle.getResponse()).setContentType(getContentType());
super.respond(requestCycle);
}
}
);
It's convenient because it converts from Object to JSON. PrettyPrinter is included because it is easy to see.
that's all. Thank you for your hard work!
Recommended Posts