Java tutorial
package i5.las2peer.services.userManagement; import java.net.HttpURLConnection; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; import com.fasterxml.jackson.core.JsonProcessingException; import i5.las2peer.api.Service; import i5.las2peer.restMapper.HttpResponse; import i5.las2peer.restMapper.MediaType; import i5.las2peer.restMapper.RESTMapper; import i5.las2peer.restMapper.annotations.ContentParam; import i5.las2peer.restMapper.annotations.Version; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import io.swagger.annotations.Contact; import io.swagger.annotations.Info; import io.swagger.annotations.License; import io.swagger.annotations.SwaggerDefinition; import io.swagger.jaxrs.Reader; import io.swagger.models.Swagger; import io.swagger.util.Json; import net.minidev.json.JSONObject; import net.minidev.json.JSONValue; /** * * iPlayService * * This microservice was generated by the CAE (Community Application Editor). If you edit it, please * make sure to keep the general structure of the file and only add the body of the methods provided * in this main file. Private methods are also allowed, but any "deeper" functionality should be * outsourced to (imported) classes. * */ @Path("user") @Version("0.1") // this annotation is used by the XML mapper @Api @SwaggerDefinition(info = @Info(title = "iPlayService", version = "0.1", description = "A LAS2peer microservice generated by the CAE.", termsOfService = "none", contact = @Contact(name = "Bender", email = "CAEAddress@gmail.com"), license = @License(name = "BSD", url = "https://github.com/CAE-Community-Application-Editor/microservice-iPlayService/blob/master/LICENSE.txt"))) public class UserManagement extends Service { public UserManagement() { // read and set properties values setFieldValues(); } // ////////////////////////////////////////////////////////////////////////////////////// // Service methods. // ////////////////////////////////////////////////////////////////////////////////////// /** * * createUser * * * @return HttpResponse * */ @POST @Path("/create") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_CONFLICT, message = "userExists"), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "authorizationNeeded"), @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "userCreated"), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "internal"), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "malformedRequest") }) @ApiOperation(value = "createUser", notes = "") public HttpResponse createUser() { // userExists boolean userExists_condition = true; if (userExists_condition) { String error = "Some String"; HttpResponse userExists = new HttpResponse(error, HttpURLConnection.HTTP_CONFLICT); return userExists; } // authorizationNeeded boolean authorizationNeeded_condition = true; if (authorizationNeeded_condition) { String error = "Some String"; HttpResponse authorizationNeeded = new HttpResponse(error, HttpURLConnection.HTTP_UNAUTHORIZED); return authorizationNeeded; } // userCreated boolean userCreated_condition = true; if (userCreated_condition) { JSONObject User = new JSONObject(); HttpResponse userCreated = new HttpResponse(User.toJSONString(), HttpURLConnection.HTTP_CREATED); return userCreated; } // internal boolean internal_condition = true; if (internal_condition) { String error = "Some String"; HttpResponse internal = new HttpResponse(error, HttpURLConnection.HTTP_INTERNAL_ERROR); return internal; } // malformedRequest boolean malformedRequest_condition = true; if (malformedRequest_condition) { String malformation = "Some String"; HttpResponse malformedRequest = new HttpResponse(malformation, HttpURLConnection.HTTP_BAD_REQUEST); return malformedRequest; } return null; } /** * * getUser * * * @return HttpResponse * */ @GET @Path("/get") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "userFound"), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "internal"), @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "userNotFound") }) @ApiOperation(value = "getUser", notes = "") public HttpResponse getUser() { // userFound boolean userFound_condition = true; if (userFound_condition) { JSONObject user = new JSONObject(); HttpResponse userFound = new HttpResponse(user.toJSONString(), HttpURLConnection.HTTP_OK); return userFound; } // internal boolean internal_condition = true; if (internal_condition) { String error = "Some String"; HttpResponse internal = new HttpResponse(error, HttpURLConnection.HTTP_INTERNAL_ERROR); return internal; } // userNotFound boolean userNotFound_condition = true; if (userNotFound_condition) { String error = "Some String"; HttpResponse userNotFound = new HttpResponse(error, HttpURLConnection.HTTP_NOT_FOUND); return userNotFound; } return null; } /** * * updateUser * * * @return HttpResponse * */ @PUT @Path("/update") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.TEXT_PLAIN) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "userNotFound"), @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "updated"), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "malformedUpdate") }) @ApiOperation(value = "updateUser", notes = "") public HttpResponse updateUser() { // userNotFound boolean userNotFound_condition = true; if (userNotFound_condition) { String error = "Some String"; HttpResponse userNotFound = new HttpResponse(error, HttpURLConnection.HTTP_NOT_FOUND); return userNotFound; } // updated boolean updated_condition = true; if (updated_condition) { JSONObject updatedUser = new JSONObject(); HttpResponse updated = new HttpResponse(updatedUser.toJSONString(), HttpURLConnection.HTTP_OK); return updated; } // malformedUpdate boolean malformedUpdate_condition = true; if (malformedUpdate_condition) { String error = "Some String"; HttpResponse malformedUpdate = new HttpResponse(error, HttpURLConnection.HTTP_BAD_REQUEST); return malformedUpdate; } return null; } // ////////////////////////////////////////////////////////////////////////////////////// // Methods required by the LAS2peer framework. // ////////////////////////////////////////////////////////////////////////////////////// /** * * This method is needed for every RESTful application in LAS2peer. Please don't change. * * @return the mapping * */ public String getRESTMapping() { String result = ""; try { result = RESTMapper.getMethodsAsXML(this.getClass()); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * Returns the API documentation of all annotated resources for purposes of Swagger documentation. * * @return The resource's documentation * */ @GET @Path("/swagger.json") @Produces(MediaType.APPLICATION_JSON) public HttpResponse getSwaggerJSON() { Swagger swagger = new Reader(new Swagger()).read(this.getClass()); if (swagger == null) { return new HttpResponse("Swagger API declaration not available!", HttpURLConnection.HTTP_NOT_FOUND); } try { return new HttpResponse(Json.mapper().writeValueAsString(swagger), HttpURLConnection.HTTP_OK); } catch (JsonProcessingException e) { e.printStackTrace(); return new HttpResponse(e.getMessage(), HttpURLConnection.HTTP_INTERNAL_ERROR); } } }