Commit 9ad35fe6 authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

query context

parent f5ebfd9a
...@@ -13,4 +13,5 @@ dependencies { ...@@ -13,4 +13,5 @@ dependencies {
// compile group: 'cglib', name: 'cglib', version: '3.2.4' // compile group: 'cglib', name: 'cglib', version: '3.2.4'
testCompile group: 'org.testng', name: 'testng', version: '6.8' testCompile group: 'org.testng', name: 'testng', version: '6.8'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
compile group: 'com.google.guava', name: 'guava', version: '19.0'
} }
package kz.arta.synergy.api;
/**
* @author raimbek
* @since 10.11.2016
*/
public class ConfigProvider {
}
package kz.arta.synergy.api;
import javax.xml.bind.DatatypeConverter;
/**
* Created by: Abzal Kultayev
* Date: 07.11.16
* Time: 15:11
*
* Контекст для запросов
*/
public class QueryContext {
/**
* Адрес сервера
*/
private String address;
/**
* Логин пользователя
*/
private String login;
/**
* Пароль пользователя
*/
private String password;
public QueryContext(String address) {
this.address = address;
}
public QueryContext(String address, String login, String password) {
this.address = address;
this.login = login;
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAuthHeader() {
return DatatypeConverter.printBase64Binary((getLogin() + ":" + getPassword()).getBytes());
}
}
package kz.arta.synergy.api;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
/**
* Created by: Abzal Kultayev
* Date: 07.11.16
* Time: 15:08
*/
public class RestHttpQuery {
public static final int TIMEOUT = 10*60*1000;
public String doGetQuery(QueryContext context, String query) throws IOException {
URL url = new URL(context.getAddress() + query);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(TIMEOUT);
conn.setReadTimeout(TIMEOUT);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json; charset=utf-8");
authentication(context, conn);
return readStringReturn(conn);
}
public String doGetQuery(QueryContext context, String query, Map<String, ?> params) throws IOException {
return doGetQuery(context, makeQuery(query, params));
}
private String makeQuery(String query, Map<String, ?> params) {
String joined = joinParamsForQuery(params);
if (!joined.isEmpty()) return query + "?" + joined;
return query;
}
private String joinParamsForQuery(Map<String, ?> params) {
if (params == null) return "";
return FluentIterable.from(params.entrySet()).transform(
new Function<Map.Entry<String, ?>, String>() {
@Override
public String apply(Map.Entry<String, ?> input) {
String value = input.getValue().toString();
try {
value = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
value = null;
}
if (value == null || value.isEmpty()) return null;
return input.getKey() + "=" + value;
}
}).filter(Predicates.notNull()).join(Joiner.on("&"));
}
private String readStringReturn(HttpURLConnection conn) throws IOException {
String output;
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((output = br.readLine()) != null) {
result.append(output);
}
conn.disconnect();
return result.toString();
}
private void authentication(QueryContext context, HttpURLConnection conn) {
if (context.getLogin() != null && context.getPassword() != null) {
conn.setRequestProperty("Authorization", "Basic " + context.getAuthHeader());
}
}
}
package kz.arta.synergy.api;
import kz.arta.synergy.api.asforms.exceptions.SynergyApiCallException;
import kz.arta.synergy.api.asforms.pojo.AsFormWrapper;
/**
* @author raimbek
* @since 09.11.2016
*/
public class SynergyApiProvider {
public AsFormWrapper getAsfData(String dataUUID) throws SynergyApiCallException {
return new AsFormWrapper();
}
public String saveData(AsFormWrapper asfData) {
return null;
}
}
package kz.arta.synergy.api.asforms; package kz.arta.synergy.api.asforms;
import kz.arta.synergy.api.SynergyApiProvider;
import kz.arta.synergy.api.asforms.annotations.*; import kz.arta.synergy.api.asforms.annotations.*;
import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException; import kz.arta.synergy.api.asforms.exceptions.CreateAsFormException;
import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException; import kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException;
...@@ -18,25 +17,20 @@ import java.util.Set; ...@@ -18,25 +17,20 @@ import java.util.Set;
*/ */
public class AsFormProvider { public class AsFormProvider {
private SynergyApiProvider synergyApiProvider;
private AsFormProvider() { private AsFormProvider() {
} }
public static AsFormProvider newInstance(SynergyApiProvider synergyApiProvider) { public static AsFormProvider newInstance() {
AsFormProvider asFormProvider = new AsFormProvider(); return new AsFormProvider();
asFormProvider.synergyApiProvider = synergyApiProvider;
return asFormProvider;
} }
public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams advancedSearchParams) { public List<AdvancedSearchResult> advancedSearch(AdvancedSearchParams advancedSearchParams) {
return new ArrayList<AdvancedSearchResult>(); return new ArrayList<AdvancedSearchResult>();
} }
public <T extends AsForm> T fetch(Class<T> formClass, String dataUUID) { /*public <T extends AsForm> T fetch(Class<T> formClass, String dataUUID) {
AsFormWrapper asfData = synergyApiProvider.getAsfData(dataUUID);
return create(formClass, asfData); return create(formClass, asfData);
} }*/
public <T extends AsForm> T create(Class<T> asFormClass, AsFormWrapper asfData) { public <T extends AsForm> T create(Class<T> asFormClass, AsFormWrapper asfData) {
T asForm = createAsFormObject(asFormClass, asfData, null); T asForm = createAsFormObject(asFormClass, asfData, null);
...@@ -47,9 +41,9 @@ public class AsFormProvider { ...@@ -47,9 +41,9 @@ public class AsFormProvider {
return asForm; return asForm;
} }
public <T extends AsForm> String save(T asForm) { /*public <T extends AsForm> String save(T asForm) {
return synergyApiProvider.saveData(toAsfData(asForm));
} }*/
public <T extends AsForm> AsFormWrapper toAsfData(T asForm) { public <T extends AsForm> AsFormWrapper toAsfData(T asForm) {
AsFormWrapper asFormWrapper = new AsFormWrapper(); AsFormWrapper asFormWrapper = new AsFormWrapper();
......
package kz.arta.synergy.api.asforms.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author raimbek
* @since 10.11.2016
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Form {
String id() default "";
String code() default "";
String config() default "";
}
...@@ -11,7 +11,6 @@ import java.util.List; ...@@ -11,7 +11,6 @@ import java.util.List;
* @author raimbek * @author raimbek
* @since 09.11.2016 * @since 09.11.2016
*/ */
@Form(config = "application_form")
public class TestForm extends AsForm { public class TestForm extends AsForm {
@TextInput("text_input") @TextInput("text_input")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment