1818import javax .ws .rs .core .Context ;
1919import javax .ws .rs .core .MediaType ;
2020
21+ import java .util .Locale ;
22+
2123import static java .nio .charset .StandardCharsets .UTF_8 ;
2224
2325@ Path ("/1.0/dashboardExport" )
@@ -111,11 +113,12 @@ private static String getWidgetName(String widgetName, JSONObject body) throws J
111113 public void downloadPdf (@ Context HttpServletRequest req ) {
112114 logger .debug ("IN" );
113115 response .setCharacterEncoding (UTF_8 .name ());
116+ Locale locale = req .getLocale ();
114117 try {
115118 JSONObject body = RestUtilities .readBodyAsJSONObject (req );
116119 String token = request .getHeader (TOKEN_HEADER );
117120 String userId = token .substring (7 );
118- DashboardPdfExporter dashboardPdfExporter = new DashboardPdfExporter (userId , body );
121+ DashboardPdfExporter dashboardPdfExporter = new DashboardPdfExporter (userId , locale );
119122 String mimeType = dashboardPdfExporter .getMimeType ();
120123
121124 if (!MimeUtils .isValidMimeType (mimeType ))
@@ -126,6 +129,26 @@ public void downloadPdf(@Context HttpServletRequest req) {
126129 data = dashboardPdfExporter .getBinaryData (body );
127130 String widgetName = body .getJSONObject ("settings" ).getJSONObject ("style" ).getJSONObject ("title" )
128131 .optString ("text" );
132+ if (widgetName != null && widgetName .startsWith ("$P{" )) {
133+ // Extract the value between {} - e.g., "$P{country}" -> "country"
134+ int startIndex = widgetName .indexOf ('{' ) + 1 ;
135+ int endIndex = widgetName .indexOf ('}' );
136+ String placeholderToReplace = (startIndex > 0 && endIndex > startIndex )
137+ ? widgetName .substring (startIndex , endIndex )
138+ : "" ;
139+ String remainingPart = widgetName .substring (endIndex + 1 );
140+ try {
141+ for (int i = 0 ; i < body .optJSONArray ("drivers" ).length (); i ++) {
142+ JSONObject driver = body .optJSONArray ("drivers" ).getJSONObject (i );
143+ if (driver .getString ("urlName" ).equals (placeholderToReplace )) {
144+ widgetName = driver .getString ("value" ) + remainingPart ;
145+ break ;
146+ }
147+ }
148+ } catch (JSONException e ) {
149+ throw new RuntimeException (e );
150+ }
151+ }
129152 response .setHeader ("Content-Disposition" , "attachment; fileName=" + widgetName + "." + "pdf" );
130153 response .setContentType (MediaType .APPLICATION_OCTET_STREAM );
131154 response .setHeader ("Content-length" , Integer .toString (data .length ));
0 commit comments