1 package com.jsql.model.injection.method;
2
3 import com.jsql.model.InjectionModel;
4 import com.jsql.model.exception.JSqlException;
5 import com.jsql.model.exception.StoppedByUserSlidingException;
6 import com.jsql.util.I18nUtil;
7 import com.jsql.util.JsonUtil;
8 import com.jsql.util.LogLevelUtil;
9 import org.apache.commons.lang3.StringUtils;
10 import org.apache.logging.log4j.LogManager;
11 import org.apache.logging.log4j.Logger;
12 import org.json.JSONException;
13
14 import java.io.Serializable;
15 import java.util.AbstractMap.SimpleEntry;
16 import java.util.List;
17 import java.util.regex.Pattern;
18
19 public abstract class AbstractMethodInjection implements Serializable {
20
21 private static final Logger LOGGER = LogManager.getRootLogger();
22
23 protected final InjectionModel injectionModel;
24
25 protected AbstractMethodInjection(InjectionModel injectionModel) {
26 this.injectionModel = injectionModel;
27 }
28
29 public abstract boolean isCheckingAllParam();
30 public abstract String getParamsAsString();
31 public abstract List<SimpleEntry<String, String>> getParams();
32 public abstract String name();
33
34 public boolean testParameters(boolean hasFoundInjection) throws JSqlException {
35 if (!hasFoundInjection) {
36 LOGGER.log(
37 LogLevelUtil.CONSOLE_DEFAULT,
38 "{} {} params...",
39 () -> I18nUtil.valueByKey("LOG_CHECKING"),
40 () -> this.name().toLowerCase()
41 );
42 return this.testParameters();
43 }
44 return true;
45 }
46
47
48
49
50
51
52
53 public boolean testParameters() throws JSqlException {
54 var hasFoundInjection = false;
55
56
57
58 if (
59 !this.injectionModel.getMediatorUtils().getPreferencesUtil().isCheckingAllParam()
60 && this.injectionModel.getMediatorUtils().getConnectionUtil().getMethodInjection() != this
61 ) {
62 return false;
63 }
64
65
66 this.injectionModel.getMediatorUtils().getConnectionUtil().setMethodInjection(this);
67
68
69 if (
70 this.getParamsAsString().contains(InjectionModel.STAR)
71 || this.injectionModel.getMediatorUtils().getConnectionUtil().getUrlBase().contains(InjectionModel.STAR)
72 ) {
73 hasFoundInjection = this.checkParamWithStar();
74 } else if (!this.isCheckingAllParam()) {
75 hasFoundInjection = this.checkLastParam();
76 } else {
77 hasFoundInjection = this.checkAllParams();
78 }
79 return hasFoundInjection;
80 }
81
82 private boolean checkParamWithStar() throws JSqlException {
83 SimpleEntry<String, String> parameterToInject = this.getParams().stream()
84 .filter(entry -> entry.getValue().contains("*"))
85 .findFirst()
86 .orElse(null);
87 return this.injectionModel.getMediatorStrategy().testStrategies(parameterToInject);
88 }
89
90
91
92
93 private boolean checkLastParam() throws JSqlException {
94
95
96
97 SimpleEntry<String, String> parameterToInject = this.getParams().stream()
98 .reduce((a, b) -> b)
99 .orElseThrow(() -> new JSqlException("Missing last parameter"));
100 return this.injectionModel.getMediatorStrategy().testStrategies(parameterToInject);
101 }
102
103
104
105
106
107
108
109 private boolean checkAllParams() throws StoppedByUserSlidingException {
110
111
112 for (SimpleEntry<String, String> paramBase: this.getParams()) {
113
114
115
116 for (SimpleEntry<String, String> paramStar: this.getParams()) {
117 if (paramStar == paramBase) {
118 try {
119 if (this.isParamInjectable(paramStar)) {
120 return true;
121 }
122 } catch (JSONException e) {
123 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
124 }
125 }
126 }
127 }
128 return false;
129 }
130
131 private boolean isParamInjectable(SimpleEntry<String, String> paramStar) throws StoppedByUserSlidingException {
132 boolean hasFoundInjection;
133
134
135 Object jsonEntity = JsonUtil.getJson(paramStar.getValue());
136
137
138 List<SimpleEntry<String, String>> attributesJson = JsonUtil.createEntries(jsonEntity, "root", null);
139
140
141
142
143 if (!attributesJson.isEmpty() && this.injectionModel.getMediatorUtils().getPreferencesUtil().isCheckingAllJsonParam()) {
144 hasFoundInjection = this.injectionModel.getMediatorUtils().getJsonUtil().testJsonParam(this, paramStar);
145 } else {
146 hasFoundInjection = this.testJsonlessParam(paramStar);
147 }
148 return hasFoundInjection;
149 }
150
151 public boolean testJsonlessParam(SimpleEntry<String, String> paramStar) throws StoppedByUserSlidingException {
152 var hasFoundInjection = false;
153
154 paramStar.setValue(paramStar.getValue() + InjectionModel.STAR);
155
156 try {
157 LOGGER.log(
158 LogLevelUtil.CONSOLE_INFORM,
159 "{} {} parameter {}={}",
160 () -> I18nUtil.valueByKey("LOG_CHECKING"),
161 this::name,
162 paramStar::getKey,
163 () -> paramStar.getValue().replace(InjectionModel.STAR, StringUtils.EMPTY)
164 );
165
166
167
168 hasFoundInjection = this.injectionModel.getMediatorStrategy().testStrategies(paramStar);
169
170 } catch (StoppedByUserSlidingException e) {
171 throw e;
172 } catch (JSqlException e) {
173 LOGGER.log(
174 LogLevelUtil.CONSOLE_ERROR,
175 "No {} injection found for parameter {}={} ({})",
176 this.name(),
177 paramStar.getKey(),
178 paramStar.getValue().replaceAll("\\+.?$|\\" + InjectionModel.STAR, StringUtils.EMPTY),
179 e.getMessage()
180 );
181 } finally {
182 if (!hasFoundInjection) {
183
184
185 this.getParams().forEach(e ->
186 e.setValue(
187 e.getValue().replaceAll(Pattern.quote(InjectionModel.STAR) +"$", StringUtils.EMPTY)
188 )
189 );
190
191
192 paramStar.setValue(paramStar.getValue().replace(InjectionModel.STAR, StringUtils.EMPTY));
193 }
194 }
195 return hasFoundInjection;
196 }
197 }