Last active
March 2, 2016 15:00
-
-
Save luizamboni/745b9b067657bc153550 to your computer and use it in GitHub Desktop.
StrongParameters in Java to filter parameters from view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
public class StrongParameters { | |
public static void main(String[] args){ | |
GenericModel model = new GenericModel(); | |
Map<String,Object> requestParams = new HashMap<String,Object>(){ | |
{ | |
put("name",new ArrayList<String>(Arrays.asList("teste"))); | |
put("categoryId",new ArrayList<String>(Arrays.asList("1111"))); | |
put("injection",new ArrayList<String>(Arrays.asList("xxx"))); | |
} | |
}; | |
; | |
StrongParameters sp1 = new StrongParameters(requestParams) | |
.require(new String[]{"name","categoryId"}) | |
.reject(new String[]{"injection"}); | |
System.out.println( model.assignAttributes(sp1)); | |
System.out.println( model); | |
model = new GenericModel(); | |
StrongParameters sp2 = new StrongParameters(requestParams) | |
.require(new String[]{"non-exist"}) | |
.reject(new String[]{"injection"}); | |
System.out.println( model.assignAttributes(sp2)); | |
System.out.println( model); | |
} | |
private HashMap<String,Object> rawParametes; | |
private HashMap<String,Object> params; | |
Boolean valid = true; | |
public Boolean isValid(){ | |
return valid; | |
} | |
public HashMap<String,Object> project() throws Exception{ | |
if(isValid()){ | |
return params; | |
}else{ | |
throw new Exception(); | |
} | |
} | |
public StrongParameters(Map<String,Object> rawParametes) { | |
this.rawParametes = (HashMap)rawParametes; | |
this.params = new HashMap<String,Object>(); | |
filterParams(); | |
} | |
private void filterParams(){ | |
for(Entry<String, Object> entry : rawParametes.entrySet()) { | |
String key = entry.getKey(); | |
ArrayList value = (ArrayList)entry.getValue(); | |
if(value.size() == 1){ | |
params.put(key, value.get(0)); | |
}else{ | |
params.put(key, value); | |
} | |
} | |
} | |
public StrongParameters require(String[] paramsNames){ | |
for(String paramName : Arrays.asList(paramsNames)){ | |
if(params.get(paramName) == null){ | |
valid = false; | |
} | |
} | |
return this; | |
} | |
public StrongParameters reject(String[] paramsNames){ | |
for(String paramName : Arrays.asList(paramsNames)){ | |
params.remove(paramName); | |
} | |
return this; | |
} | |
} | |
class GenericModel{ | |
String name; | |
Integer categoryId; | |
String injected; | |
public String toString(){ | |
return "name:"+name + ", categoryId:" + categoryId + ", injected:" + injected; | |
} | |
//isso aqui pode armazenar aos valores em variáveis temporárias para caso ocorra um erro | |
// não | |
public Boolean assignAttributes(StrongParameters parameters){ | |
HashMap<String,Object> params; | |
String _name; | |
Integer _categoryId; | |
String _injected; | |
try { | |
params = parameters.project(); | |
if(params.get("name") != null){ | |
_name = (String) params.get("name"); | |
} | |
if(params.get("categoryId") != null){ | |
_categoryId = Integer.valueOf((String)params.get("categoryId")); | |
} | |
if(params.get("injected") != null){ | |
_injected = (String) params.get("injected"); | |
} | |
name = _name; | |
categoryId = _categoryId; | |
injected = _injected; | |
return true; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment