Search in sources :

Example 16 with StatusCodes

use of org.codehaus.enunciate.jaxrs.StatusCodes in project pentaho-platform by pentaho.

the class ThemeResource method setTheme.

/**
 * Set the current theme to the one provided in this request
 *
 * @param theme (theme to be changed to)
 *
 * @return
 */
@POST
@Path("/set")
@Consumes({ WILDCARD })
@StatusCodes({ @ResponseCode(code = 200, condition = "Successfully set theme."), @ResponseCode(code = 403, condition = "Illegal set operation.") })
@Produces("text/plain")
@Facet(name = "Unsupported")
public Response setTheme(String theme) {
    IThemeManager themeManager = PentahoSystem.get(IThemeManager.class);
    List<String> ids = themeManager.getSystemThemeIds();
    if ((ids != null) && (ids.indexOf(theme) >= 0)) {
        getPentahoSession().setAttribute("pentaho-user-theme", theme);
        IUserSettingService settingsService = PentahoSystem.get(IUserSettingService.class, getPentahoSession());
        settingsService.setUserSetting("pentaho-user-theme", theme);
        return getActiveTheme();
    } else {
        // Prevent log forging/injection
        String cleanTheme = theme.replace('\n', ' ').replace('\r', ' ');
        // We do not want to NLS-ize this message
        logger.error("Attempt to set invalid theme: " + cleanTheme);
        return Response.status(Response.Status.FORBIDDEN).entity("").build();
    }
}
Also used : IUserSettingService(org.pentaho.platform.api.usersettings.IUserSettingService) IThemeManager(org.pentaho.platform.api.ui.IThemeManager) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) StatusCodes(org.codehaus.enunciate.jaxrs.StatusCodes) Facet(org.codehaus.enunciate.Facet)

Example 17 with StatusCodes

use of org.codehaus.enunciate.jaxrs.StatusCodes in project data-access by pentaho.

the class JDBCDatasourceResource method addOrUpdate.

/**
 * Add or update a JDBC datasource connection
 *
 * <p><b>Example Request:</b><br />
 *    PUT pentaho/plugin/data-access/api/datasource/jdbc/connection/TestDatasource
 * </p>
 * <br /><b>POST data:</b>
 *  <pre function="syntax.xml">
 *    {
 *      "changed": true,
 *      "usingConnectionPool": true,
 *      "connectSql": "",
 *      "databaseName": "SampleData",
 *      "databasePort": "9001",
 *      "hostname": "localhost",
 *      "name": "TestDataSourceResource",
 *      "password": "password",
 *      "username": "pentaho_user",
 *      "attributes": {},
 *      "connectionPoolingProperties": {},
 *      "extraOptions": {},
 *      "accessType": "NATIVE",
 *      "databaseType": {
 *        "defaultDatabasePort": 9001,
 *        "extraOptionsHelpUrl": "http://hsqldb.sourceforge.net/doc/guide/ch04.html#N109DA",
 *        "name": "Hypersonic",
 *        "shortName": "HYPERSONIC",
 *        "supportedAccessTypes": [
 *          "NATIVE",
 *          "ODBC",
 *          "JNDI"
 *        ]
 *      }
 *    }
 *  </pre>
 * </p>
 *
 * @param connection A DatabaseConnection in JSON representation
 *
 * @return A jax-rs Response object with the appropriate status code, header, and body.
 *
 * <p><b>Example Response:</b></p>
 *    <pre function="syntax.xml">
 *      This response does not contain data.
 *    </pre>
 */
@PUT
@Path("/{connectionId : .+}")
@Consumes({ APPLICATION_JSON })
@StatusCodes({ @ResponseCode(code = 200, condition = "JDBC datasource added successfully."), @ResponseCode(code = 403, condition = "User is not authorized to add JDBC datasources."), @ResponseCode(code = 304, condition = "Datasource was not modified"), @ResponseCode(code = 500, condition = "An unexected error occurred while adding the JDBC datasource.") })
public Response addOrUpdate(@PathParam("connectionId") String connectionName, DatabaseConnection connection) {
    try {
        validateAccess();
        // Prefer the path name over the one in the DTO object
        connection.setId(connectionName);
        IDatabaseConnection savedConn = null;
        try {
            savedConn = service.getConnectionByName(connectionName);
        } catch (ConnectionServiceException e) {
        // unfortunatley getConnectionById throws an exception not returning null when the conneciton is not present.
        } catch (NullPointerException e) {
        // unfortunatley getConnectionById throws an exception not returning null when the conneciton is not present.
        }
        boolean success = false;
        if (savedConn != null) {
            if (StringUtils.isBlank(connection.getPassword())) {
                connection.setPassword(savedConn.getPassword());
            }
            connection.setId(savedConn.getId());
            success = service.updateConnection(connection);
        } else {
            success = service.addConnection(connection);
        }
        if (success) {
            return buildOkResponse();
        } else {
            return buildNotModifiedResponse();
        }
    } catch (PentahoAccessControlException t) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
    } catch (Throwable t) {
        logger.error("Error " + t.getMessage());
        return buildServerErrorResponse();
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) WebApplicationException(javax.ws.rs.WebApplicationException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) StatusCodes(org.codehaus.enunciate.jaxrs.StatusCodes) PUT(javax.ws.rs.PUT)

Aggregations

Path (javax.ws.rs.Path)17 StatusCodes (org.codehaus.enunciate.jaxrs.StatusCodes)17 Produces (javax.ws.rs.Produces)15 Consumes (javax.ws.rs.Consumes)8 GET (javax.ws.rs.GET)8 ArrayList (java.util.ArrayList)5 POST (javax.ws.rs.POST)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 PUT (javax.ws.rs.PUT)4 Facet (org.codehaus.enunciate.Facet)4 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 List (java.util.List)2 Properties (java.util.Properties)2 BadRequestException (javax.ws.rs.BadRequestException)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)2