Required code in Java Goal: Study a representation of a computer network to dete
ID: 3838396 • Letter: R
Question
Required code in Java
Goal: Study a representation of a computer network to determine which type of network topology exists bus, ring, or star. Provide a RESTful service which accepts a s a POST of JSON a representation of a network of computers. Assume each connection between computers is bidirectional. Output a JSON message indicating if the topology is a bus (1), a ring (2), or a star (3). If the topology is none of these, then output the type as irregular. Example input: {inList: [ {connected: [A, B]}, {connected: [B, C]}, {connected: [C, D]}, {connected: [D, E]} ]} Example output: {type: bus} Erroneous input (e.g, malformed JSON) should be handled gracefully. Submit to the Blackboard by the due date: An HTTP URL to a RESTful service which must remain up and running 24/7 until grading is complete Graders will invoke your service with a tool such as curl or Postman at a time of their choosing. A ZIP file containing your source code, written in any language you choose.Explanation / Answer
The bellow code will satisfy the requirements of above question:-
----------------------------------------------------------------------------------------------
NetworkDetails.html:-
------------------------------------
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<title>Network Details</title>
</head>
<body>
<form name="f1" action="/RestFulDemo/rest/NetworkInfoService" method="post">
Network connections <input type="text" title="enter value" name="network"/><br>
<input type="submit" value="Create">
</form>
</body>
</html>
GetNetwork.html:-
-----------------------
<html>
<head>
<script>
var xmlHttp
function showCust(str)
{
xmlHttp= new XMLHttpRequest();
var url="/RestFulDemo/rest/NetworkInfoService/getNetworkJSON/"+str;
xmlHttp.open("GET",url,true);//it will call 'NetworkDetails' servlet
xmlHttp.onreadystatechange=stateChanged;//this is userdefined method which will fired based readyState property
xmlHttp.send();
}
function stateChanged()
{
//check whether the response is completely loaded or not?
if (xmlHttp.readyState==4)
{
var str1=xmlHttp.responseText;
var obj = JSON.parse(str1);
document.f1.mail.value=obj.mail;
}
}
</script>
</head>
<body>
<form name="f1" action="/">
Network connections <input type="text" title="enter value" name="network"/><br>
</form>
</body>
</html>
GetNetworkInfo.java:-
------------------------------
package com.mss.rest.basics;
import com.google.gson.Gson;
import java.util.HashMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("NetworkInfoService")
public class NetworkInfo {
public static HashMap custMap = new HashMap();
//http://localhost:8084/RestFulDemo/rest/NetworkInfoService/getNetworkJSON/
@GET
@Path("/getNetworkJSON/{n}")
@Produces("application/json")
@Consumes("application/json")
public String getInfoJSON(@PathParam("n") String Connected) {
Network cust = (Network) custMap.get(Connected);
do{
if(n++==n)
{
return "Bus";
}
else if(n++==n && n==n)
{
return "Ring";
}
else if(n==Connected)
{
return "Star";
}
else{
return "irregular";
}
}
Gson gs = new Gson();
String custJSON = gs.toJson(cust);
return custJSON;
}
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String newNetwork(@FormParam("Connected") String na,
@FormParam("Connected") String email) {
Network cust = new Network();
custMap.put(na, cust);
return "success";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.