Commit f6d27a3b authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

new types and services

parent 86e24d85
...@@ -131,4 +131,8 @@ public class RestHttpQuery { ...@@ -131,4 +131,8 @@ public class RestHttpQuery {
conn.setRequestProperty("Authorization", "Basic " + context.getAuthHeader()); conn.setRequestProperty("Authorization", "Basic " + context.getAuthHeader());
} }
} }
public QueryContext getContext() {
return context;
}
} }
package kz.arta.synergy.api.pojo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import java.util.List;
/**
* @author raimbek
* @since 30.01.2017
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class DocumentFullAttachments {
private List<Attachment> attachments;
@JsonProperty("work_files")
private List<Attachment> workFiles;
public List<Attachment> getAttachments() {
return attachments;
}
public void setAttachments(List<Attachment> attachments) {
this.attachments = attachments;
}
public List<Attachment> getWorkFiles() {
return workFiles;
}
public void setWorkFiles(List<Attachment> workFiles) {
this.workFiles = workFiles;
}
}
...@@ -5,6 +5,7 @@ import kz.arta.synergy.api.Query; ...@@ -5,6 +5,7 @@ import kz.arta.synergy.api.Query;
import kz.arta.synergy.api.QueryContext; import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.RestHttpQuery; import kz.arta.synergy.api.RestHttpQuery;
import kz.arta.synergy.api.pojo.DocSend; import kz.arta.synergy.api.pojo.DocSend;
import kz.arta.synergy.api.pojo.DocumentFullAttachments;
import kz.arta.synergy.api.pojo.DocumentInfo; import kz.arta.synergy.api.pojo.DocumentInfo;
import java.io.IOException; import java.io.IOException;
...@@ -14,6 +15,8 @@ import java.io.IOException; ...@@ -14,6 +15,8 @@ import java.io.IOException;
* @since 14.11.2016 * @since 14.11.2016
*/ */
public class DocflowService { public class DocflowService {
public static String ATTACHMENT_CONTAINER = "ase:attachmentContainer";
public static String WORK_CONTAINER = "ase:workContainer";
private final RestHttpQuery restHttpQuery; private final RestHttpQuery restHttpQuery;
...@@ -33,10 +36,15 @@ public class DocflowService { ...@@ -33,10 +36,15 @@ public class DocflowService {
} }
public String createDocAttachment(String documentID, String fileName, String filePath) throws IOException { public String createDocAttachment(String documentID, String fileName, String filePath) throws IOException {
return createDocAttachment(documentID, null, fileName, filePath);
}
public String createDocAttachment(String documentID, String path, String fileName, String filePath) throws IOException {
Query query = Query.newInstance() Query query = Query.newInstance()
.methodPost() .methodPost()
.url("/rest/api/docflow/doc/attachment/create") .url("/rest/api/docflow/doc/attachment/create")
.formParam("documentID", documentID) .formParam("documentID", documentID)
.formParam("path", path)
.formParam("fileName", fileName) .formParam("fileName", fileName)
.formParam("filePath", filePath); .formParam("filePath", filePath);
return restHttpQuery.doQuery(query); return restHttpQuery.doQuery(query);
...@@ -50,4 +58,11 @@ public class DocflowService { ...@@ -50,4 +58,11 @@ public class DocflowService {
.body(JsonUtils.toJson(docSend)); .body(JsonUtils.toJson(docSend));
return restHttpQuery.doQuery(query); return restHttpQuery.doQuery(query);
} }
public DocumentFullAttachments getFullAttachments(String documentId) throws IOException {
Query query = Query.newInstance()
.url("/rest/api/docflow/doc/attachments")
.queryParam("documentID", documentId);
return JsonUtils.read(restHttpQuery.doQuery(query), DocumentFullAttachments.class);
}
} }
...@@ -9,7 +9,11 @@ import kz.arta.synergy.api.pojo.Attachment; ...@@ -9,7 +9,11 @@ import kz.arta.synergy.api.pojo.Attachment;
import kz.arta.synergy.api.pojo.AttachmentList; import kz.arta.synergy.api.pojo.AttachmentList;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
import java.io.IOException; import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
/** /**
...@@ -153,4 +157,76 @@ public class StorageService { ...@@ -153,4 +157,76 @@ public class StorageService {
.queryParam("inline", "true"); .queryParam("inline", "true");
return restHttpQuery.doQueryAndReturnBytes(query); return restHttpQuery.doQueryAndReturnBytes(query);
} }
public String upload(String fileName, InputStream inputStream, String serverTmpFile) {
String boundary = "qweasdzxczxcasdqwe";
String crlf = "\r\n";
String twoHyphens = "--";
try {
int partSize = 1024*100;
byte[] bytes = new byte[partSize];
int position = 0;
int read = 0;
StringBuilder stringBuilder = new StringBuilder();
URL url = new URL(restHttpQuery.getContext().getAddress() + "/rest/api/storage/upload_part?file=" + serverTmpFile);
while ((read = inputStream.read(bytes, 0, partSize)) != -1){
byte [] readBytes = new byte[read];
System.arraycopy(bytes, 0, readBytes, 0, read);
position += read;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json; charset=utf-8");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Authorization", "Basic "+restHttpQuery.getContext().getAuthHeader());
conn.setUseCaches (false);
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream request = new DataOutputStream(conn.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"body\";filename=\"" + URLEncoder.encode(fileName, "utf-8")+ "\"" + crlf);
request.writeBytes(crlf);
byte[] fileBytes = DatatypeConverter.printBase64Binary(readBytes).getBytes();
request.write(fileBytes);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
request.flush();
request.close ();
InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
stringBuilder.append(response.toString());
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
} }
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