View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.restapi.v1;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.StringWriter;
24  import java.nio.charset.StandardCharsets;
25  import java.util.Locale;
26  import java.util.Properties;
27  
28  import org.jdom2.Document;
29  import org.jdom2.Element;
30  import org.jdom2.output.Format;
31  import org.jdom2.output.XMLOutputter;
32  import org.mycore.common.config.MCRProperties;
33  import org.mycore.services.i18n.MCRTranslation;
34  
35  import com.google.gson.stream.JsonWriter;
36  
37  import jakarta.servlet.http.HttpServletRequest;
38  import jakarta.ws.rs.DefaultValue;
39  import jakarta.ws.rs.GET;
40  import jakarta.ws.rs.Path;
41  import jakarta.ws.rs.PathParam;
42  import jakarta.ws.rs.Produces;
43  import jakarta.ws.rs.QueryParam;
44  import jakarta.ws.rs.core.Context;
45  import jakarta.ws.rs.core.MediaType;
46  import jakarta.ws.rs.core.Response;
47  import jakarta.ws.rs.core.UriInfo;
48  import jakarta.ws.rs.core.Response.Status;
49  
50  /**
51   * REST API for messages.
52   * Allows access to message properties of the application
53   * 
54   *  
55   * @author Robert Stephan
56   * 
57   * @version $Revision: $ $Date: $
58   */
59  @Path("/messages")
60  public class MCRRestAPIMessages {
61  
62      public static final String FORMAT_JSON = "json";
63  
64      public static final String FORMAT_XML = "xml";
65  
66      public static final String FORMAT_PROPERTY = "property";
67  
68      /**
69       * lists all message properties for a given language
70       * 
71       * @param info - the injected Jersey Context Object for URI
72       * 
73       * @param request - the injected HTTPServletRequest object
74       * 
75       * @param lang - the language in which the messages should be returned (default: 'de')
76       *      
77       * @param format
78       * 		Possible values are: props (default) | json | xml
79       * @param filter
80       * 		';'-separated list of message key prefixes
81       * 
82       * @return a Jersey Response object
83       * 
84       */
85      @GET
86      @Produces({ MediaType.TEXT_XML + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8",
87          MediaType.TEXT_PLAIN + ";charset=ISO-8859-1" })
88      public Response listMessages(@Context UriInfo info, @Context HttpServletRequest request,
89          @QueryParam("lang") @DefaultValue("de") String lang,
90          @QueryParam("format") @DefaultValue("property") String format,
91          @QueryParam("filter") @DefaultValue("") String filter) {
92          Locale locale = Locale.forLanguageTag(lang);
93          String[] check = filter.split(";");
94  
95          Properties data = new MCRProperties();
96          for (String prefix : check) {
97              data.putAll(MCRTranslation.translatePrefix(prefix, locale));
98          }
99          try {
100             if (FORMAT_PROPERTY.equals(format)) {
101                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
102                 data.store(baos, "MyCoRe Messages (charset='ISO-8859-1')");
103                 return Response.ok(baos.toByteArray()).type("text/plain; charset=ISO-8859-1").build();
104             }
105             if (FORMAT_XML.equals(format)) {
106                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
107                 data.storeToXML(baos, "MyCoRe Messages");
108                 return Response.ok(baos.toString(StandardCharsets.UTF_8)).type("application/xml; charset=UTF-8")
109                     .build();
110             }
111             if (FORMAT_JSON.equals(format)) {
112                 StringWriter sw = new StringWriter();
113 
114                 JsonWriter writer = new JsonWriter(sw);
115                 writer.setIndent("    ");
116                 writer.beginObject();
117                 writer.name("messages");
118                 writer.beginObject();
119                 for (Object key : data.keySet()) {
120                     writer.name(key.toString());
121                     writer.value(data.getProperty(key.toString()));
122                 }
123                 writer.endObject();
124                 writer.endObject();
125                 writer.close();
126 
127                 return Response.ok(sw.toString()).type("application/json; charset=UTF-8")
128                     .build();
129             }
130         } catch (IOException e) {
131             //toDo
132         }
133         return Response.status(Response.Status.BAD_REQUEST).build();
134     }
135 
136     /**
137      * returns a single messages entry.
138      * 
139      * @param info - the injected Jersey context object for URI
140      * @param request - the injected HTTPServletRequest object
141      * @param key - the message key
142      * @param lang - the language
143      * @param format 
144      *     Possible values are: props (default) | json | xml (required)
145      * @return a Jersey Response Object
146      */
147     @GET
148     @Path("/{value}")
149     @Produces({ MediaType.TEXT_XML + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8",
150         MediaType.TEXT_PLAIN + ";charset=UTF-8" })
151     public Response getMessage(@Context UriInfo info, @Context HttpServletRequest request,
152         @PathParam("value") String key, @QueryParam("lang") @DefaultValue("de") String lang,
153         @QueryParam("format") @DefaultValue("text") String format) {
154         Locale locale = Locale.forLanguageTag(lang);
155         String result = MCRTranslation.translate(key, locale);
156         try {
157             if (FORMAT_PROPERTY.equals(format)) {
158                 return Response.ok(key + "=" + result).type("text/plain; charset=ISO-8859-1")
159                     .build();
160             }
161             if (FORMAT_XML.equals(format)) {
162                 Document doc = new Document();
163                 Element root = new Element("entry");
164                 root.setAttribute("key", key);
165                 root.setText(result);
166                 doc.addContent(root);
167                 StringWriter sw = new StringWriter();
168                 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
169                 outputter.output(doc, sw);
170                 return Response.ok(sw.toString()).type("application/xml; charset=UTF-8")
171                     .build();
172             }
173             if (FORMAT_JSON.equals(format)) {
174                 StringWriter sw = new StringWriter();
175                 JsonWriter writer = new JsonWriter(sw);
176                 writer.setIndent("    ");
177                 writer.beginObject();
178                 writer.name(key);
179                 writer.value(result);
180                 writer.endObject();
181                 writer.close();
182                 return Response.ok(sw.toString()).type("application/json; charset=UTF-8")
183                     .build();
184             }
185             //text only
186             return Response.ok(result).type("text/plain; charset=UTF-8")
187                 .build();
188         } catch (IOException e) {
189             //toDo
190         }
191         return Response.status(Status.BAD_REQUEST).build();
192     }
193 }