Medium Vulnerabilities CodeFix: 1. URL Redirection 2. AAF-1111
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / facade / LocateFacadeImpl.java
index f41fcd9..047663c 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -59,13 +59,14 @@ import org.onap.aaf.misc.env.Env;
 import org.onap.aaf.misc.env.TimeTaken;
 import org.onap.aaf.misc.rosetta.env.RosettaDF;
 import org.onap.aaf.misc.rosetta.env.RosettaData;
+import org.owasp.encoder.Encode;
 
 import locate_local.v1_0.Api;
 
 
 /**
  * AuthzFacade
- * 
+ *
  * This Service Facade encapsulates the essence of the API Service can do, and provides
  * a single created object for elements such as RosettaDF.
  *
@@ -76,14 +77,14 @@ import locate_local.v1_0.Api;
  *         a) In the future, we may support multiple Response Formats, aka JSON or XML, based on User Request.
  * 4) Log Service info, warnings and exceptions as necessary
  * 5) When asked by the API layer, this will create and write Error content to the OutputStream
- * 
- * Note: This Class does NOT set the HTTP Status Code.  That is up to the API layer, so that it can be 
+ *
+ * Note: This Class does NOT set the HTTP Status Code.  That is up to the API layer, so that it can be
  * clearly coordinated with the API Documentation
- * 
+ *
  * @author Jonathan
  *
  */
-public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURATION,ERROR> extends FacadeImpl implements LocateFacade 
+public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURATION,ERROR> extends FacadeImpl implements LocateFacade
     {
     private LocateService<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURATION,ERROR> service;
 
@@ -105,21 +106,21 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
         (mepDF                = env.newDataFactory(mapper().getClass(API.MGMT_ENDPOINTS))).in(dataType).out(dataType);
         (confDF                = env.newDataFactory(mapper().getClass(API.CONFIG))).in(dataType).out(dataType);
     }
-    
+
     public Mapper<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURATION,ERROR> mapper() {
         return service.mapper();
     }
-        
+
     /* (non-Javadoc)
      * @see com.att.authz.facade.AuthzFacade#error(org.onap.aaf.auth.env.test.AuthzTrans, javax.servlet.http.HttpServletResponse, int)
-     * 
+     *
      * Note: Conforms to AT&T TSS RESTful Error Structure
      */
     @Override
     public void error(AuthzTrans trans, HttpServletResponse response, Result<?> result) {
         String msg = result.details==null?"":result.details.trim();
         String[] detail;
-        if(result.variables==null) {
+        if (result.variables==null) {
             detail = new String[1];
         } else {
             int l = result.variables.length;
@@ -128,11 +129,11 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
         }
         error(trans, response, result.status,msg,detail);
     }
-        
+
     @Override
     public void error(AuthzTrans trans, HttpServletResponse response, int status, String msg, String ... _detail) {
             String[] detail = _detail;
-        if(detail.length==0) {
+        if (detail.length==0) {
             detail=new String[1];
         }
         boolean hidemsg = false;
@@ -153,7 +154,7 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
                 detail[0] = "Forbidden";
                 response.setStatus(/*httpstatus=*/403);
                 break;
-                
+
             case 404:
             case ERR_NotFound:
                 msgId = "SVC1404";
@@ -167,21 +168,21 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
                 detail[0] = "Not Acceptable";
                 response.setStatus(/*httpstatus=*/406);
                 break;
-                
+
             case 409:
             case ERR_ConflictAlreadyExists:
                 msgId = "SVC1409";
                 detail[0] = "Conflict Already Exists";
                 response.setStatus(/*httpstatus=*/409);
                 break;
-            
+
             case 501:
             case ERR_NotImplemented:
                 msgId = "SVC1501";
-                detail[0] = "Not Implemented"; 
+                detail[0] = "Not Implemented";
                 response.setStatus(/*httpstatus=*/501);
                 break;
-                
+
             default:
                 msgId = "SVC1500";
                 detail[0] = "General Service Error";
@@ -194,22 +195,22 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
             StringBuilder holder = new StringBuilder();
             ERROR em = mapper().errorFromMessage(holder,msgId,msg,detail);
             trans.checkpoint(
-                    "ErrResp [" + 
+                    "ErrResp [" +
                     msgId +
                     "] " +
                     holder.toString(),
                     Env.ALWAYS);
-            if(hidemsg) {
+            if (hidemsg) {
                 holder.setLength(0);
                 em = mapper().errorFromMessage(holder, msgId, "Server had an issue processing this request");
             }
             errDF.newData(trans).load(em).to(response.getOutputStream());
-            
+
         } catch (Exception e) {
             trans.error().log(e,"unable to send response for",msg);
         }
     }
-    
+
     /* (non-Javadoc)
      * @see com.att.authz.facade.AuthzFacade#getAPI(org.onap.aaf.auth.env.test.AuthzTrans, javax.servlet.http.HttpServletResponse)
      */
@@ -221,25 +222,25 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
             Api api = new Api();
             Api.Route ar;
             Method[] meths = LocateServiceImpl.class.getDeclaredMethods();
-            for(RouteReport rr : rservlet.routeReport()) {
+            for (RouteReport rr : rservlet.routeReport()) {
                 api.getRoute().add(ar = new Api.Route());
                 ar.setMeth(rr.meth.name());
                 ar.setPath(rr.path);
                 ar.setDesc(rr.desc);
                 ar.getContentType().addAll(rr.contextTypes);
-                for(Method m : meths) {
+                for (Method m : meths) {
                     ApiDoc ad;
-                    if((ad = m.getAnnotation(ApiDoc.class))!=null &&
+                    if ((ad = m.getAnnotation(ApiDoc.class))!=null &&
                             rr.meth.equals(ad.method()) &&
                             rr.path.equals(ad.path())) {
-                        for(String param : ad.params()) {
+                        for (String param : ad.params()) {
                             ar.getParam().add(param);
                         }
-                        for(String text : ad.text()) {
+                        for (String text : ad.text()) {
                             ar.getComments().add(text);
                         }
                         ar.setExpected(ad.expectedCode());
-                        for(int ec : ad.errorCodes()) {
+                        for (int ec : ad.errorCodes()) {
                             ar.getExplicitErr().add(ec);
                         }
                     }
@@ -256,7 +257,7 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
             tt.done();
         }
     }
-    
+
     public final static String API_EXAMPLE = "apiExample";
     /* (non-Javadoc)
      * @see com.att.authz.facade.AuthzFacade#getAPIExample(org.onap.aaf.auth.env.test.AuthzTrans, javax.servlet.http.HttpServletResponse, java.lang.String)
@@ -265,8 +266,8 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
     public Result<Void> getAPIExample(AuthzTrans trans, HttpServletResponse resp, String nameOrContentType, boolean optional) {
         TimeTaken tt = trans.start(API_EXAMPLE, Env.SUB);
         try {
-            String content =Examples.print(apiDF.getEnv(), nameOrContentType, optional); 
-            resp.getOutputStream().print(content);
+            String content =Examples.print(apiDF.getEnv(), nameOrContentType, optional);
+            resp.getOutputStream().print(Encode.forJava(content));
             setContentType(resp,content.contains("<?xml")?TYPE.XML:TYPE.JSON);
             return Result.ok();
         } catch (Exception e) {
@@ -289,20 +290,20 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
             String output=null;
             long temp=System.currentTimeMillis();
             synchronized(LOCK) {
-                if(cacheClear<temp) {
+                if (cacheClear<temp) {
                     epsCache.clear();
                     cacheClear = temp+1000*60*2; // 2 mins standard cache clear
                 } else {
                     output = epsCache.get(key);
-                    if("{}".equals(output) && emptyCheck<temp) {
+                    if ("{}".equals(output) && emptyCheck<temp) {
                         output = null;
-                        emptyCheck = temp+5000; // 5 second check  
+                        emptyCheck = temp+5000; // 5 second check
                     }
                 }
             }
-            if(output==null) {
+            if (output==null) {
                 Result<ENDPOINTS> reps = this.service.getEndPoints(trans,service,version,other);
-                if(reps.notOK()) {
+                if (reps.notOK()) {
                     return Result.err(reps);
                 } else {
                     output = epDF.newData(trans).load(reps.value).asString();
@@ -311,7 +312,7 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
                     }
                 }
             }
-            resp.getOutputStream().println(output);
+            resp.getOutputStream().println(Encode.forJava(output));
             setContentType(resp,epDF.getOutType());
             return Result.ok();
         } catch (Exception e) {
@@ -334,14 +335,14 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
             try {
                 RosettaData<MGMT_ENDPOINTS> data = mepDF.newData().load(req.getInputStream());
                 rreq = data.asObject();
-            } catch(APIException e) {
+            } catch (APIException e) {
                 trans.error().log("Invalid Input",IN,PUT_MGMT_ENDPOINTS);
                 return Result.err(Status.ERR_BadData,"Invalid Input");
 
             }
             Result<Void> rp = service.putMgmtEndPoints(trans, rreq);
             switch(rp.status) {
-                case OK: 
+                case OK:
                     synchronized(LOCK) {
                         cacheClear = 0L;
                     }
@@ -370,14 +371,14 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
             try {
                 RosettaData<MGMT_ENDPOINTS> data = mepDF.newData().load(req.getInputStream());
                 rreq = data.asObject();
-            } catch(APIException e) {
+            } catch (APIException e) {
                 trans.error().log("Invalid Input",IN,DELETE_MGMT_ENDPOINTS);
                 return Result.err(Status.ERR_BadData,"Invalid Input");
 
             }
             Result<Void> rp = service.removeMgmtEndPoints(trans, rreq);
             switch(rp.status) {
-                case OK: 
+                case OK:
                     synchronized(LOCK) {
                         cacheClear = 0L;
                     }
@@ -401,7 +402,7 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
         try {
             Result<CONFIGURATION> rp = service.getConfig(trans, id, type);
             switch(rp.status) {
-                case OK: 
+                case OK:
                     setContentType(resp,mepDF.getOutType());
                     confDF.newData(trans).load(rp.value).to(resp.getOutputStream());
                     return Result.ok();
@@ -413,7 +414,7 @@ public abstract class LocateFacadeImpl<IN,OUT,ENDPOINTS,MGMT_ENDPOINTS,CONFIGURA
             return Result.err(e);
         } finally {
             tt.done();
-        }    
+        }
     }
-    
+
 }
\ No newline at end of file