Files
WidgetWorkbench/backend/target/site/jacoco/com.workbenchclassic.hmi/HmiStyleResource.java.html
2026-10-20 20:22:38 +00:00

96 lines
13 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="de"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>HmiStyleResource.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">backend</a> &gt; <a href="index.source.html" class="el_package">com.workbenchclassic.hmi</a> &gt; <span class="el_source">HmiStyleResource.java</span></div><h1>HmiStyleResource.java</h1><pre class="source lang-java linenums">package com.workbenchclassic.hmi;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import org.json.JSONArray;
import org.json.JSONObject;
/** Manage FULL ios.css snapshots in cust/CUST (no mutation of the deployed WAR). */
@Path(&quot;/hmi/v1/styles&quot;)
@Produces(MediaType.APPLICATION_JSON)
<span class="nc" id="L31">public final class HmiStyleResource {</span>
@Context private SecurityContext security;
@Context private ServletContext servletContext;
@Context private HttpServletRequest request;
<span class="nc bnc" id="L35" title="All 2 branches missed."> private String user(){return security.getUserPrincipal()==null?null:security.getUserPrincipal().getName();}</span>
<span class="nc bnc" id="L36" title="All 6 branches missed."> private boolean designer(){return security.isUserInRole(&quot;hmi-admin&quot;)||security.isUserInRole(&quot;hmi-designer&quot;)||security.isUserInRole(&quot;hmi-publisher&quot;);}</span>
<span class="nc bnc" id="L37" title="All 4 branches missed."> private boolean publisher(){return security.isUserInRole(&quot;hmi-admin&quot;)||security.isUserInRole(&quot;hmi-publisher&quot;);}</span>
<span class="nc bnc" id="L38" title="All 8 branches missed."> private boolean csrf(){HttpSession s=request.getSession(false);if(s==null)return false;Object token=s.getAttribute(&quot;HMI_CSRF&quot;);String got=request.getHeader(&quot;X-CSRF-Token&quot;);return token instanceof String&amp;&amp;got!=null&amp;&amp;java.security.MessageDigest.isEqual(((String)token).getBytes(java.nio.charset.StandardCharsets.UTF_8),got.getBytes(java.nio.charset.StandardCharsets.UTF_8));}</span>
<span class="nc" id="L39"> private Response json(Object obj){return Response.ok(obj.toString(),MediaType.APPLICATION_JSON).header(&quot;Cache-Control&quot;,&quot;no-store&quot;).build();}</span>
<span class="nc" id="L40"> private Response fail(int status,String reason){return Response.status(status).type(MediaType.APPLICATION_JSON).entity(new JSONObject().put(&quot;error&quot;,reason).toString()).build();}</span>
<span class="nc bnc" id="L41" title="All 14 branches missed."> private Response gate(boolean write,boolean publish){if(user()==null)return fail(401,&quot;GlassFish login required&quot;);if(!HmiSchemaBootstrap.isReady())return fail(503,&quot;HMI database not initialized&quot;);if(!(publish?publisher():designer()))return fail(403,&quot;Role required&quot;);if(write&amp;&amp;!csrf())return fail(403,&quot;CSRF token invalid&quot;);return null;}</span>
<span class="nc" id="L42"> private String table(){return HmiDb.schemaPrefix()+&quot;HMI_APP_CSS_REVISION&quot;;}</span>
<span class="nc bnc" id="L43" title="All 2 branches missed."> private static String read(ResultSet rs,int col)throws Exception {try(Reader r=rs.getCharacterStream(col)){return r==null?&quot;&quot;:HmiStyles.read(r);}}</span>
<span class="nc" id="L44"> private static void content(PreparedStatement p,int i,String value)throws SQLException{p.setCharacterStream(i,new StringReader(value),value.length());}</span>
private static String publishedId(Connection c,String table)throws SQLException{
<span class="nc bnc" id="L46" title="All 2 branches missed."> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID FROM &quot;+table+&quot; WHERE STATUS='PUBLISHED' ORDER BY VERSION_NO DESC&quot;);ResultSet rs=p.executeQuery()) {return rs.next()?rs.getString(1):&quot;&quot;;}</span>
}
@GET @Path(&quot;ios&quot;)
<span class="nc bnc" id="L49" title="All 2 branches missed."> public Response view(){Response g=gate(false,false);if(g!=null)return g;try(Connection c=HmiDb.open()){</span>
<span class="nc" id="L50"> JSONObject out=new JSONObject().put(&quot;original&quot;,HmiStyles.original(servletContext));JSONArray versions=new JSONArray();</span>
<span class="nc" id="L51"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT ID, VERSION_NO, STATUS, UPDATED_BY, UPDATED_AT FROM &quot;+table()+&quot; ORDER BY VERSION_NO DESC&quot;);ResultSet rs=p.executeQuery()){</span>
<span class="nc bnc" id="L52" title="All 4 branches missed."> int count=0;while(rs.next()&amp;&amp;count++&lt;60)versions.put(new JSONObject().put(&quot;id&quot;,rs.getString(1)).put(&quot;version&quot;,rs.getInt(2)).put(&quot;status&quot;,rs.getString(3)).put(&quot;author&quot;,rs.getString(4)).put(&quot;updatedAt&quot;,String.valueOf(rs.getTimestamp(5))));}</span>
<span class="nc" id="L53"> out.put(&quot;versions&quot;,versions).put(&quot;publishedId&quot;,publishedId(c,table()));</span>
<span class="nc" id="L54"> String published=null;</span>
<span class="nc" id="L55"> try(PreparedStatement p=c.prepareStatement(&quot;SELECT CSS_CONTENT FROM &quot;+table()+&quot; WHERE STATUS='PUBLISHED' ORDER BY VERSION_NO DESC&quot;)){</span>
<span class="nc bnc" id="L56" title="All 2 branches missed."> try(ResultSet rs=p.executeQuery()){if(rs.next())published=read(rs,1);}</span>
}
<span class="nc bnc" id="L58" title="All 2 branches missed."> out.put(&quot;published&quot;,published==null?out.getString(&quot;original&quot;):published);</span>
<span class="nc" id="L59"> return json(out);</span>
<span class="nc" id="L60"> }catch(Exception ex){servletContext.log(&quot;CSS read failed&quot;,ex);return fail(500,&quot;Stylesheet unavailable&quot;);}}</span>
@GET @Path(&quot;ios/{id}&quot;)
<span class="nc bnc" id="L62" title="All 4 branches missed."> public Response version(@PathParam(&quot;id&quot;)String id){Response g=gate(false,false);if(g!=null)return g;if(!id.matches(&quot;[A-Za-z0-9-]{1,64}&quot;))return fail(400,&quot;Invalid revision&quot;);</span>
<span class="nc bnc" id="L63" title="All 2 branches missed."> try(Connection c=HmiDb.open();PreparedStatement p=c.prepareStatement(&quot;SELECT CSS_CONTENT FROM &quot;+table()+&quot; WHERE ID=?&quot;)){p.setString(1,id);try(ResultSet rs=p.executeQuery()){return rs.next()?json(new JSONObject().put(&quot;id&quot;,id).put(&quot;css&quot;,read(rs,1))):fail(404,&quot;Revision missing&quot;);}}</span>
<span class="nc" id="L64"> catch(Exception ex){servletContext.log(&quot;CSS version failed&quot;,ex);return fail(500,&quot;Read failed&quot;);}}</span>
@POST @Path(&quot;ios/drafts&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L66" title="All 2 branches missed."> public Response draft(String jsonBody){Response g=gate(true,false);if(g!=null)return g;</span>
<span class="nc" id="L67"> try{JSONObject input=new JSONObject(jsonBody);String css=input.getString(&quot;css&quot;);HmiStyles.validate(css);</span>
<span class="nc" id="L68"> try(Connection c=HmiDb.open()){c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);c.setAutoCommit(false);</span>
<span class="nc bnc" id="L69" title="All 2 branches missed."> try{int next=1;try(PreparedStatement p=c.prepareStatement(&quot;SELECT MAX(VERSION_NO) FROM &quot;+table());ResultSet rs=p.executeQuery()){if(rs.next())next=rs.getInt(1)+1;}</span>
<span class="nc" id="L70"> String id=UUID.randomUUID().toString();try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+table()+&quot;(ID,VERSION_NO,STATUS,CSS_CONTENT,UPDATED_BY,UPDATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L71"> p.setString(1,id);p.setInt(2,next);p.setString(3,&quot;DRAFT&quot;);content(p,4,css);p.setString(5,user());p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();}</span>
<span class="nc" id="L72"> audit(c,&quot;ios.css.draft&quot;,id);c.commit();return json(new JSONObject().put(&quot;id&quot;,id).put(&quot;version&quot;,next));</span>
<span class="nc" id="L73"> }catch(Exception ex){c.rollback();throw ex;}</span>
}
<span class="nc" id="L75"> }catch(IllegalArgumentException ex){return fail(400,ex.getMessage());}</span>
<span class="nc" id="L76"> catch(Exception ex){servletContext.log(&quot;CSS draft failed&quot;,ex);return fail(500,&quot;Draft could not be saved&quot;);}}</span>
@POST @Path(&quot;ios/{id}/publish&quot;) @Consumes(MediaType.APPLICATION_JSON)
<span class="nc bnc" id="L78" title="All 2 branches missed."> public Response publish(@PathParam(&quot;id&quot;)String id,String jsonBody){Response g=gate(true,true);if(g!=null)return g;</span>
<span class="nc bnc" id="L79" title="All 2 branches missed."> if(!id.matches(&quot;[A-Za-z0-9-]{1,64}&quot;))return fail(400,&quot;Invalid revision&quot;);</span>
<span class="nc" id="L80"> try{JSONObject body=new JSONObject(jsonBody);String expected=body.optString(&quot;expectedPublishedId&quot;,&quot;&quot;);</span>
<span class="nc" id="L81"> try(Connection c=HmiDb.open()){c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);c.setAutoCommit(false);</span>
try{
<span class="nc bnc" id="L83" title="All 2 branches missed."> String actual=publishedId(c,table());if(!actual.equals(expected)){c.rollback();return fail(409,&quot;Published revision has changed; reload before publishing&quot;);}</span>
<span class="nc bnc" id="L84" title="All 6 branches missed."> try(PreparedStatement exists=c.prepareStatement(&quot;SELECT STATUS FROM &quot;+table()+&quot; WHERE ID=?&quot;)){exists.setString(1,id);try(ResultSet rs=exists.executeQuery()){if(!rs.next()){c.rollback();return fail(404,&quot;Unknown revision&quot;);}}}</span>
<span class="nc" id="L85"> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+table()+&quot; SET STATUS='ARCHIVED' WHERE STATUS='PUBLISHED'&quot;)){p.executeUpdate();}</span>
<span class="nc bnc" id="L86" title="All 2 branches missed."> try(PreparedStatement p=c.prepareStatement(&quot;UPDATE &quot;+table()+&quot; SET STATUS='PUBLISHED' WHERE ID=?&quot;)){p.setString(1,id);if(p.executeUpdate()!=1)throw new SQLException(&quot;Revision not found&quot;);}</span>
<span class="nc" id="L87"> audit(c,&quot;ios.css.published&quot;,id);c.commit();return json(new JSONObject().put(&quot;ok&quot;,true).put(&quot;id&quot;,id));</span>
<span class="nc" id="L88"> }catch(Exception e){c.rollback();throw e;}</span>
<span class="nc bnc" id="L89" title="All 4 branches missed."> }</span>
<span class="nc" id="L90"> }catch(Exception ex){servletContext.log(&quot;CSS publish failed&quot;,ex);return fail(500,&quot;Publish failed&quot;);}}</span>
private void audit(Connection c,String action,String id)throws SQLException{
<span class="nc" id="L92"> try(PreparedStatement p=c.prepareStatement(&quot;INSERT INTO &quot;+HmiDb.schemaPrefix()+&quot;HMI_APP_AUDIT(ID,ACTOR,ACTION_NAME,ENTITY_ID,DETAILS,CREATED_AT) VALUES(?,?,?,?,?,?)&quot;)){</span>
<span class="nc" id="L93"> p.setString(1,UUID.randomUUID().toString());p.setString(2,user());p.setString(3,action);p.setString(4,id);p.setString(5,&quot;Online style editor&quot;);p.setTimestamp(6,Timestamp.from(Instant.now()));p.executeUpdate();}</span>
<span class="nc" id="L94"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.4.201905082037</span></div></body></html>