Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm using Intellij for a project using Java. I have included the following code. I need to know how to convert HashMap to JSON. How

I'm using Intellij for a project using Java. I have included the following code. I need to know how to convert HashMap to JSON. How can I do this? //Response Header Imports import java.net.*; import java.util.*; import java.io.IOException; ///////////////////////////////// import com.commercebank.serveranalyzer.BackEnd.DataPieces.CertChainData; import com.commercebank.serveranalyzer.BackEnd.DataPieces.CipherSuiteData; import com.commercebank.serveranalyzer.BackEnd.DataPieces.DataPiece; import com.commercebank.serveranalyzer.BackEnd.DataPieces.ResponseHeaderData; import org.springframework.web.servlet.ModelAndView; import java.util.Map; import java.security.cert.X509Certificate; import java.net.URL; import javax.net.ssl.*; import java.security.cert.Certificate; import java.net.MalformedURLException; public class ServerAnalyzerBackEnd { private String url; private boolean validURL; private ArrayList dataToFetch; public ServerAnalyzerBackEnd(String url) { this.url = url; this.validURL = validateURLSyntax(); this.dataToFetch = new ArrayList<>(); } // Add all data pieces to fetch public void fetchAll() { dataToFetch.add(new ResponseHeaderData()); dataToFetch.add(new CertChainData()); dataToFetch.add(new CipherSuiteData()); } //add a single data piece to be fetched public void addDataPieceToFetch(DataPiece dataPiece) { dataToFetch.add(dataPiece); } public ModelAndView start() { String host; Map params = new HashMap<>(); params.put("url", url); String secureURL = ""; URL testURL; int port; String cert_chain = ""; if (!validURL) { System.out.println("We should probably stop it here or something."); } //url is not null, assign to secureURL to create https connection if(url != null){ secureURL = url; } //Initialize sesson and connection SSLSession session = null; HttpsURLConnection connection = null; //create URL object from 'url' //create https connection based off of url and then call getCertChain method to pull that cert chain try { testURL = new URL(secureURL); port = testURL.getPort(); host = testURL.getHost(); SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); Socket socket = socketFactory.createSocket(host, port); session = ((SSLSocket) socket).getSession(); connection = (HttpsURLConnection)testURL.openConnection(); } catch(MalformedURLException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } // fetch data for all data pieces for (DataPiece dataPiece : dataToFetch) { dataPiece.fetchData(connection, session); } //put all data pieces into hashmap for (DataPiece dataPiece : dataToFetch) { params.put(dataPiece.getDataName(), dataPiece.getDataResult()); } return new ModelAndView("showURL", params); } public boolean validateURLSyntax() { int firstColon; int secondColon; boolean URLsyntax = true; //Checks if the first four characters are http if (!this.url.substring(0,4).equals("http")) { URLsyntax = false; } //Checks if a port number was specified. If not, appends :443 to the URL. firstColon = this.url.indexOf(":"); secondColon = this.url.lastIndexOf(':'); if (secondColon == -1 || firstColon == secondColon) { this.url = this.url + ":443"; } //Something like https://www.google.com: and no port actually specified or junk is after the port. secondColon = this.url.lastIndexOf(':'); if (this.url.charAt(this.url.length() - 1) == ':' || !Character.isDigit(this.url.charAt(secondColon + 1))) { URLsyntax = false; } return URLsyntax; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions