|
| 1 | +package cz.incad.kramerius.rest.api.k5.client.info; |
| 2 | + |
| 3 | +import com.google.inject.Inject; |
| 4 | +import com.google.inject.Provider; |
| 5 | +import cz.incad.kramerius.rest.api.exceptions.GenericApplicationException; |
| 6 | +import cz.incad.kramerius.service.ResourceBundleService; |
| 7 | +import cz.incad.kramerius.service.TextsService; |
| 8 | +import cz.incad.kramerius.utils.conf.KConfiguration; |
| 9 | +import net.sf.json.JSONObject; |
| 10 | + |
| 11 | +import javax.ws.rs.*; |
| 12 | +import javax.ws.rs.core.MediaType; |
| 13 | +import javax.ws.rs.core.Response; |
| 14 | +import java.awt.*; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.util.Locale; |
| 18 | +import java.util.Properties; |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +/** |
| 22 | + * Created by Martin Rumanek on 27.5.15. |
| 23 | + */ |
| 24 | + |
| 25 | +@Path("/v5.0/info") |
| 26 | +public class InfoResource { |
| 27 | + |
| 28 | + private static final String DEFAULT_INTRO_CONSTANT = "default_intro"; |
| 29 | + |
| 30 | + private static final String INTRO_CONSTANT = "intro"; |
| 31 | + |
| 32 | + private static final String RIGHT_MSG = "rightMsg"; |
| 33 | + |
| 34 | + public static Logger LOGGER = Logger.getLogger(InfoResource.class.getName()); |
| 35 | + |
| 36 | + @Inject |
| 37 | + private KConfiguration configuration; |
| 38 | + |
| 39 | + @Inject |
| 40 | + private TextsService textService; |
| 41 | + |
| 42 | + @Inject |
| 43 | + private Provider<Locale> provider; |
| 44 | + |
| 45 | + @Inject |
| 46 | + private ResourceBundleService resourceBundleService; |
| 47 | + |
| 48 | + InputStream revisions = this.getClass().getClassLoader().getResourceAsStream("build.properties"); |
| 49 | + |
| 50 | + @GET |
| 51 | + @Produces({MediaType.APPLICATION_JSON + ";charset=utf-8"}) |
| 52 | + public Response getInfo() { |
| 53 | + |
| 54 | + Properties buildProperties = new Properties(); |
| 55 | + String version = ""; |
| 56 | + String hash = ""; |
| 57 | + try { |
| 58 | + buildProperties.load(revisions); |
| 59 | + version = buildProperties.getProperty("version"); |
| 60 | + hash = buildProperties.getProperty("hash"); |
| 61 | + } catch (IOException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + |
| 65 | + JSONObject jsonObject = new JSONObject(); |
| 66 | + jsonObject.put("version", version); |
| 67 | + jsonObject.put("hash", hash); |
| 68 | + |
| 69 | + String adminEmail = configuration.getProperty("administrator.email"); |
| 70 | + if (adminEmail != null && !adminEmail.isEmpty()) { |
| 71 | + jsonObject.put("email", adminEmail); |
| 72 | + } |
| 73 | + |
| 74 | + String intro = null; |
| 75 | + String rightsMsg = null; |
| 76 | + try { |
| 77 | + if (textService.isAvailable(INTRO_CONSTANT, provider.get())) { |
| 78 | + intro = textService.getText(INTRO_CONSTANT, provider.get()); |
| 79 | + } else { |
| 80 | + intro = textService.getText(DEFAULT_INTRO_CONSTANT, provider.get()); |
| 81 | + } |
| 82 | + if (intro != null && !intro.isEmpty()) { |
| 83 | + jsonObject.put("intro", intro); |
| 84 | + } |
| 85 | + |
| 86 | + if (textService.isAvailable(RIGHT_MSG, provider.get())) { |
| 87 | + rightsMsg = textService.getText(RIGHT_MSG, provider.get()); |
| 88 | + } else { |
| 89 | + rightsMsg = resourceBundleService.getResourceBundle("labels", provider.get()).getString(RIGHT_MSG); |
| 90 | + } |
| 91 | + |
| 92 | + if (intro != null && !intro.isEmpty()) { |
| 93 | + jsonObject.put("intro", intro); |
| 94 | + } |
| 95 | + |
| 96 | + if (rightsMsg != null && !rightsMsg.isEmpty()) { |
| 97 | + jsonObject.put("rightMsg", rightsMsg); |
| 98 | + } |
| 99 | + |
| 100 | + } catch (IOException e) { |
| 101 | + throw new GenericApplicationException(e.getMessage()); |
| 102 | + } |
| 103 | + |
| 104 | + String maxPage = KConfiguration.getInstance().getProperty( |
| 105 | + "generatePdfMaxRange"); |
| 106 | + boolean turnOff = KConfiguration.getInstance().getConfiguration().getBoolean("turnOffPdfCheck"); |
| 107 | + |
| 108 | + if (turnOff) { |
| 109 | + jsonObject.put("pdfMaxRange", "unlimited"); |
| 110 | + } else { |
| 111 | + jsonObject.put("pdfMaxRange", maxPage); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + return Response.ok().entity(jsonObject.toString()).build(); |
| 116 | + } |
| 117 | +} |
0 commit comments