KomodoConnectionService extends KomodoService { @POST @Path( StringConstants.FORWARD_SLASH + V1Constants.CONNECTION_PLACEHOLDER ) @Produces( MediaType.APPLICATION_JSON ) @ApiOperation(value = "Create a connection in the workspace") @ApiResponses(value = { @ApiResponse(code = 406, message = "Only JSON is returned by this operation"), @ApiResponse(code = 403, message = "An error has occurred.") }) public Response createConnection( final @Context HttpHeaders headers, final @Context UriInfo uriInfo, @ApiParam( value = "Name of the connection", required = true ) final @PathParam( "connectionName" ) String connectionName, @ApiParam( value = "" + "JSON of the properties of the new connection:<br>" + OPEN_PRE_TAG + OPEN_BRACE + BR + NBSP + "driverName: \"name of the driver, eg. mysql\"" + COMMA + BR + NBSP + "jndiName: \"the jndi name of the connection\"" + COMMA + BR + NBSP + "jdbc: \"true if jdbc, otherwise false\"" + BR + NBSP + "parameters: " + OPEN_BRACE + BR + NBSP + NBSP + "property1...n: \"key/value pairs of properties applicable to connection" + BR + NBSP + CLOSE_BRACE + BR + CLOSE_BRACE + CLOSE_PRE_TAG, required = true ) final String connectionJson) throws KomodoRestException { SecurityPrincipal principal = checkSecurityContext(headers); if (principal.hasErrorResponse()) return principal.getErrorResponse(); List<MediaType> mediaTypes = headers.getAcceptableMediaTypes(); if (! isAcceptable(mediaTypes, MediaType.APPLICATION_JSON_TYPE)) return notAcceptableMediaTypesBuilder().build(); if (StringUtils.isBlank( connectionName )) { return createErrorResponseWithForbidden(mediaTypes, RelationalMessages.Error.CONNECTION_SERVICE_CREATE_MISSING_NAME); } RestConnection restConnection = new RestConnection(); restConnection.setId(connectionName); final KomodoConnectionAttributes rcAttr; try { rcAttr = KomodoJsonMarshaller.unmarshall(connectionJson, KomodoConnectionAttributes.class); restConnection.setDriverName(rcAttr.getDriver()); restConnection.setJndiName(rcAttr.getJndi()); restConnection.setJdbc(rcAttr.isJdbc()); for (Map.Entry<String, Object> entry : rcAttr.getParameters().entrySet()) { restConnection.addProperty(entry.getKey(), entry.getValue()); } } catch (Exception ex) { throw new KomodoRestException(ex); } UnitOfWork uow = null; try { uow = createTransaction(principal, "createConnection", false ); if ( getWorkspaceManager(uow).hasChild( uow, connectionName ) ) { return createErrorResponseWithForbidden(mediaTypes, RelationalMessages.Error.CONNECTION_SERVICE_CREATE_ALREADY_EXISTS); } return doAddConnection( uow, uriInfo.getBaseUri(), mediaTypes, restConnection ); } catch (final Exception e) { if ((uow != null) && (uow.getState() != State.ROLLED_BACK)) { uow.rollback(); } if (e instanceof KomodoRestException) { throw (KomodoRestException)e; } return createErrorResponseWithForbidden(mediaTypes, e, RelationalMessages.Error.CONNECTION_SERVICE_CREATE_CONNECTION_ERROR, connectionName); } } KomodoConnectionService( final KEngine engine ); @GET @Produces( MediaType.APPLICATION_JSON ) @ApiOperation(value = "Return the collection of connections", response = RestConnection[].class) @ApiResponses(value = { @ApiResponse(code = 403, message = "An error has occurred.") }) Response getConnections( final @Context HttpHeaders headers,
                                    final @Context UriInfo uriInfo ); @GET @Path( V1Constants.CONNECTION_PLACEHOLDER ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @ApiOperation(value = "Find connection by name", response = RestConnection.class) @ApiResponses(value = { @ApiResponse(code = 404, message = "No Connection could be found with name"), @ApiResponse(code = 406, message = "Only JSON or XML is returned by this operation"), @ApiResponse(code = 403, message = "An error has occurred.") }) Response getConnection( final @Context HttpHeaders headers,
                                   final @Context UriInfo uriInfo,
                                   @ApiParam(
                                             value = "Name of the connection",
                                             required = true
                                   )
                                   final @PathParam( "connectionName" ) String connectionName); @POST @Path( StringConstants.FORWARD_SLASH + V1Constants.CONNECTION_PLACEHOLDER ) @Produces( MediaType.APPLICATION_JSON ) @ApiOperation(value = "Create a connection in the workspace") @ApiResponses(value = { @ApiResponse(code = 406, message = "Only JSON is returned by this operation"), @ApiResponse(code = 403, message = "An error has occurred.") }) Response createConnection( final @Context HttpHeaders headers,
                                      final @Context UriInfo uriInfo,
                                      @ApiParam(
                                                value = "Name of the connection",
                                                required = true
                                      )
                                      final @PathParam( "connectionName" ) String connectionName,
                                      @ApiParam(
                                                value = "" + 
                                                        "JSON of the properties of the new connection:<br>" +
                                                        OPEN_PRE_TAG +
                                                        OPEN_BRACE + BR +
                                                        NBSP + "driverName: \"name of the driver, eg. mysql\"" + COMMA + BR +
                                                        NBSP + "jndiName: \"the jndi name of the connection\"" + COMMA + BR +
                                                        NBSP + "jdbc: \"true if jdbc, otherwise false\"" + BR +
                                                        NBSP + "parameters: " + OPEN_BRACE + BR +
                                                        NBSP + NBSP + "property1...n: \"key/value pairs of properties applicable to connection" + BR +
                                                        NBSP + CLOSE_BRACE + BR +
                                                        CLOSE_BRACE +
                                                        CLOSE_PRE_TAG,
                                                required = true
                                      )
                                      final String connectionJson); @POST @Path( StringConstants.FORWARD_SLASH + V1Constants.CLONE_SEGMENT + StringConstants.FORWARD_SLASH + V1Constants.CONNECTION_PLACEHOLDER ) @Produces( MediaType.APPLICATION_JSON ) @ApiOperation(value = "Clone a connection in the workspace") @ApiResponses(value = { @ApiResponse(code = 406, message = "Only JSON is returned by this operation"), @ApiResponse(code = 403, message = "An error has occurred.") }) Response cloneConnection( final @Context HttpHeaders headers,
                                     final @Context UriInfo uriInfo,
                                     @ApiParam(
                                               value = "Name of the connection",
                                               required = true
                                     )
                                     final @PathParam( "connectionName" ) String connectionName,
                                     @ApiParam(
                                               value = "The new name of the connection",
                                               required = true
                                     )
                                     final String newConnectionName); @PUT @Path( StringConstants.FORWARD_SLASH + V1Constants.CONNECTION_PLACEHOLDER ) @Produces( MediaType.APPLICATION_JSON ) @ApiOperation(value = "Update a connection in the workspace") @ApiResponses(value = { @ApiResponse(code = 406, message = "Only JSON is returned by this operation"), @ApiResponse(code = 403, message = "An error has occurred.") }) Response updateConnection( final @Context HttpHeaders headers,
                                      final @Context UriInfo uriInfo,
                                      @ApiParam(
                                                value = "Name of the connection",
                                                required = true
                                      )
                                      final @PathParam( "connectionName" ) String connectionName,
                                      @ApiParam(
                                                value = "" + 
                                                        "JSON of the properties of the connection:<br>" +
                                                        OPEN_PRE_TAG +
                                                        OPEN_BRACE + BR +
                                                        NBSP + "driverName: \"name of the driver, eg. mysql\"" + COMMA + BR +
                                                        NBSP + "jndiName: \"the jndi name of the connection\"" + COMMA + BR +
                                                        NBSP + "jdbc: \"true if jdbc, otherwise false\"" + BR +
                                                        NBSP + "parameters: " + OPEN_BRACE + BR +
                                                        NBSP + NBSP + "property1...n: \"key/value pairs of properties applicable to connection" + BR +
                                                        NBSP + CLOSE_BRACE + BR +
                                                        CLOSE_BRACE +
                                                        CLOSE_PRE_TAG,
                                                required = true
                                      )
                                      final String connectionJson); @DELETE @Path("{connectionName}") @Produces( MediaType.APPLICATION_JSON ) @ApiOperation(value = "Delete a connection from the workspace") @ApiResponses(value = { @ApiResponse(code = 406, message = "Only JSON is returned by this operation"), @ApiResponse(code = 403, message = "An error has occurred.") }) Response deleteConnection( final @Context HttpHeaders headers,
                                      final @Context UriInfo uriInfo,
                                      @ApiParam(
                                                value = "Name of the connection",
                                                required = true
                                      )
                                      final @PathParam( "connectionName" ) String connectionName); @GET @Path( V1Constants.NAME_VALIDATION_SEGMENT + StringConstants.FORWARD_SLASH + V1Constants.CONNECTION_PLACEHOLDER ) @Produces( { MediaType.TEXT_PLAIN } ) @ApiOperation( value = "Returns an error message if the Connection name is invalid" ) @ApiResponses( value = { @ApiResponse( code = 400, message = "The URI cannot contain encoded slashes or backslashes." ), @ApiResponse( code = 403, message = "An unexpected error has occurred." ), @ApiResponse( code = 500, message = "The Connection name cannot be empty." ) } ) Response validateConnectionName( final @Context HttpHeaders headers,
                                     final @Context UriInfo uriInfo,
                                     @ApiParam( value = "The Connection name being checked", required = true )
                                     final @PathParam( "connectionName" ) String connectionName );  }
@Test public void shouldGetConnections() throws Exception { createConnection(CONNECTION_NAME); URI uri = _uriBuilder.workspaceConnectionsUri(); ClientRequest request = request(uri, MediaType.APPLICATION_JSON_TYPE); ClientResponse<String> response = request.get(String.class); assertNotNull(response.getEntity()); final String entities = response.getEntity(); assertThat(entities, is(notNullValue())); RestConnection[] connection = KomodoJsonMarshaller.unmarshallArray(entities, RestConnection[].class); assertEquals(1, connection.length); RestConnection mySource = connection[0]; assertNotNull(mySource.getId()); assertTrue(CONNECTION_NAME.equals(mySource.getId())); assertNotNull(mySource.getDataPath()); assertNotNull(mySource.getkType()); }
@Test public void shouldGetConnection() throws Exception { createConnection(CONNECTION_NAME); Properties settings = _uriBuilder.createSettings(SettingNames.CONNECTION_NAME, CONNECTION_NAME); _uriBuilder.addSetting(settings, SettingNames.PARENT_PATH, _uriBuilder.workspaceConnectionsUri()); URI uri = _uriBuilder.connectionUri(LinkType.SELF, settings); ClientRequest request = request(uri, MediaType.APPLICATION_JSON_TYPE); ClientResponse<String> response = request.get(String.class); final String entity = response.getEntity(); assertThat(entity, is(notNullValue())); System.out.println("Response:\n" + entity); RestConnection connection = KomodoJsonMarshaller.unmarshall(entity, RestConnection.class); assertNotNull(connection); assertEquals(connection.getId(), CONNECTION_NAME); }
@Test public void shouldUpdateConnection() throws Exception { createConnection(CONNECTION_NAME); Properties settings = _uriBuilder.createSettings(SettingNames.CONNECTION_NAME, CONNECTION_NAME); _uriBuilder.addSetting(settings, SettingNames.PARENT_PATH, _uriBuilder.workspaceConnectionsUri()); URI uri = _uriBuilder.connectionUri(LinkType.SELF, settings); ClientRequest request = request(uri, MediaType.APPLICATION_JSON_TYPE); KomodoConnectionAttributes rcAttr = new KomodoConnectionAttributes(); rcAttr.setJndi("jndi:/MySqlDS1"); rcAttr.setDriver("mysql"); rcAttr.setJdbc(true); rcAttr.setParameter("username", "test"); rcAttr.setParameter("password", "myPassword"); addBody(request, rcAttr); ClientResponse<String> response = request.put(String.class); final String entity = response.getEntity(); assertThat(entity, is(notNullValue())); System.out.println("Response:\n" + entity); RestConnection rsObj = KomodoJsonMarshaller.unmarshall(entity, RestConnection.class); assertEquals(rcAttr.getDriver(), rsObj.getDriverName()); assertEquals(rcAttr.getJndi(), rsObj.getJndiName()); assertEquals(rcAttr.isJdbc(), rsObj.isJdbc()); List<RestProperty> rsProps = rsObj.getProperties(); for (Entry<String, Object> parameter : rcAttr.getParameters().entrySet()) { RestProperty rsProp = null; for (RestProperty rsp : rsProps) { if (! rsp.getName().equals(parameter.getKey())) continue; rsProp = rsp; } assertNotNull(parameter.getKey() + " property not handled", rsProp); assertEquals(parameter.getValue(), rsProp.getValue()); } }