Merge authors: Stian Sandvold (stian-sandvold) ------------------------------------------------------------ revno: 20504 [merge] committer: Stian Sandvold <stian.sandv...@gmail.com> branch nick: dhis2 timestamp: Mon 2015-10-05 10:51:34 +0200 message: Resolved a bug where casting systemSetting to boolean threw an exception modified: dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/StaticContentController.java
-- lp:dhis2 https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk Your team DHIS 2 developers is subscribed to branch lp:dhis2. To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/StaticContentController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/StaticContentController.java 2015-09-25 09:54:36 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/StaticContentController.java 2015-10-05 08:48:44 +0000 @@ -47,6 +47,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; +import org.springframework.util.MimeType; +import org.springframework.util.MimeTypeUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -56,9 +58,9 @@ import com.google.common.collect.ImmutableMap; /** - * Serves and uploads custom images(PNG) for the logo on the frontpage (logo_front) + * Serves and uploads custom images for the logo on the front page (logo_front) * and for the logo on the top banner (logo_banner). - * + * * @author Stian Sandvold */ @Controller @@ -82,27 +84,25 @@ put( LOGO_FRONT, SystemSettingManager.KEY_USE_CUSTOM_LOGO_FRONT ).build(); /** - * Serves a PNG associated with the key. if custom logo is not used, the - * request will redirect to the default logos. + * Serves the PNG associated with the key. If custom logo is not used the + * request will redirect to the default. * - * @param key key associated with the file\image. - * @param response the response associated with the request. + * @param key key associated with the file. * @throws WebMessageException */ @RequestMapping( value = "/{key}", method = RequestMethod.GET ) public void getStaticContent( - @PathVariable( "key" ) String key, - HttpServletResponse response ) + @PathVariable( "key" ) String key, HttpServletResponse response ) throws WebMessageException { if ( !KEY_WHITELIST_MAP.containsKey( key ) ) { - throw new WebMessageException( WebMessageUtils.notFound( "This key does not exist" ) ); + throw new WebMessageException( WebMessageUtils.notFound( "Key does not exist" ) ); } - Boolean useCustomFile = (Boolean) systemSettingManager.getSystemSetting( KEY_WHITELIST_MAP.get( key ), false ); + Boolean useCustomFile = Boolean.parseBoolean( (String) systemSettingManager.getSystemSetting( KEY_WHITELIST_MAP.get( key ) ) ); - if ( useCustomFile == null || !useCustomFile ) // Serve the default logos + if ( !useCustomFile ) // Serve the default { try { @@ -113,7 +113,7 @@ throw new WebMessageException( WebMessageUtils.error( "Can't read the file." ) ); } } - else // Serve the custom logos + else // Serve the custom { InputStream in = null; @@ -131,7 +131,8 @@ catch ( IOException e ) { throw new WebMessageException( - WebMessageUtils.error( "Error occured trying to serve file." ) ); + WebMessageUtils.error( "Error occurred trying to serve file.", + "An IOException was thrown, indicating a file I/O or networking error." ) ); } finally { @@ -143,8 +144,8 @@ /** * Uploads PNG images based on a key. Only accepts PNG and white listed keys. * - * @param key to associate with the image. - * @param file associated with the key. + * @param key the key + * @param file the image file * @throws WebMessageException * @throws IOException */ @@ -156,48 +157,55 @@ { if ( file == null || file.isEmpty() ) { - throw new WebMessageException( WebMessageUtils.badRequest( "Missing parameter \"file\"" ) ); + throw new WebMessageException( WebMessageUtils.badRequest( "Missing parameter 'file'" ) ); } - // Only PNG accepted currently - - if ( !file.getContentType().equalsIgnoreCase( "image/png" ) ) + // Only PNG is accepted at the current time. Ensure file is a PNG image. + MimeType mimeType = MimeTypeUtils.parseMimeType( file.getContentType() ); + + if( !mimeType.isCompatibleWith( MimeTypeUtils.IMAGE_PNG )) { - throw new WebMessageException( - new WebMessage(WebMessageStatus.WARNING, HttpStatus.UNSUPPORTED_MEDIA_TYPE ) ); + throw new WebMessageException( new WebMessage(WebMessageStatus.WARNING, HttpStatus.UNSUPPORTED_MEDIA_TYPE ) ); } - // Only keys in the white list accepted currently - + // Only keys in the white list are accepted at the current time if ( !KEY_WHITELIST_MAP.containsKey( key ) ) { throw new WebMessageException( - WebMessageUtils.badRequest( "This key is not yet supported" ) ); - } - - File out = locationManager.getFileForWriting( key + ".png", "static" ); + WebMessageUtils.badRequest( "This key is not supported." ) ); + } + + File out; + + try + { + out = locationManager.getFileForWriting( key + ".png", "static" ); + } + catch( LocationManagerException e) + { + throw new WebMessageException( WebMessageUtils.error(e.getMessage()) ); + } try { file.transferTo( out ); } - catch ( IOException e ) + catch( IOException e) { - throw new WebMessageException( (WebMessageUtils - .error( "Error saving file, make sure dhis_home envoirement variable is set" )) ); + throw new WebMessageException( WebMessageUtils.error( "Could not save file." ) ); } } /** * Returns the relative url of the default logo for a given key. * - * @param key the key associated with the logo. + * @param key the key associated with the logo or null if the key does not exist. * @return the relative url of the logo. */ private String getDefaultLogoUrl( String key ) { String relativeUrlToImage = null; - + if ( key.equals( LOGO_BANNER ) ) { relativeUrlToImage = "/dhis-web-commons/css/" + styleManager.getCurrentStyleDirectory() + "/logo_banner.png";
_______________________________________________ Mailing list: https://launchpad.net/~dhis2-devs Post to : dhis2-devs@lists.launchpad.net Unsubscribe : https://launchpad.net/~dhis2-devs More help : https://help.launchpad.net/ListHelp