Skip to content

Commit c0f09fe

Browse files
committed
added fix for file loading in case of building with war command
1 parent 0ddb1e3 commit c0f09fe

File tree

2 files changed

+169
-186
lines changed

2 files changed

+169
-186
lines changed

src/main/java/com/docusign/controller/webForms/services/CreateAndEmbedFormService.java

Lines changed: 98 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -12,160 +12,152 @@
1212
import com.docusign.webforms.model.WebFormValues;
1313

1414
import java.io.IOException;
15-
import java.net.URISyntaxException;
1615
import java.nio.file.Files;
1716
import java.nio.file.Paths;
1817
import java.util.List;
1918
import java.util.Map;
20-
import java.util.Objects;
19+
20+
import org.springframework.core.io.ClassPathResource;
21+
import org.springframework.util.StreamUtils;
2122

2223
public final class CreateAndEmbedFormService {
2324
public static WebFormSummaryList getForms(
24-
ApiClient apiClient,
25-
String userAccessToken,
26-
String search
27-
) throws ApiException {
28-
//ds-snippet-start:WebForms1Step3
29-
FormManagementApi formManagementApi = new FormManagementApi(apiClient);
25+
ApiClient apiClient,
26+
String userAccessToken,
27+
String search) throws ApiException {
28+
// ds-snippet-start:WebForms1Step3
29+
FormManagementApi formManagementApi = new FormManagementApi(apiClient);
3030
var option = formManagementApi.new ListFormsOptions();
3131
option.setSearch(search);
3232

3333
return formManagementApi.listForms(userAccessToken, option);
34-
//ds-snippet-end:WebForms1Step3
34+
// ds-snippet-end:WebForms1Step3
3535
}
3636

3737
public static void addTemplateIdToForm(String fileName, String templateId) {
3838
String targetString = "template-id";
3939

4040
try {
41-
var templateFilePath = CreateAndEmbedFormService.class.getClassLoader().getResource(fileName);
42-
var templateFile = Paths.get(Objects.requireNonNull(templateFilePath).toURI());
41+
ClassPathResource resource = new ClassPathResource(fileName);
42+
byte[] buffer = StreamUtils.copyToByteArray(resource.getInputStream());
4343

44-
String fileContent = new String(Files.readAllBytes(templateFile));
44+
String fileContent = new String(buffer);
4545
String modifiedContent = fileContent.replace(targetString, templateId);
4646

4747
var basePath = System.getProperty("user.dir");
4848
var configFile = Paths.get(basePath, "demo_documents", fileName);
4949

5050
Files.createDirectories(Paths.get(basePath, "demo_documents"));
5151
Files.write(configFile, modifiedContent.getBytes());
52-
} catch (IOException | URISyntaxException ex) {
52+
} catch (IOException ex) {
5353
System.out.println("An error occurred: " + ex.getMessage());
5454
}
5555
}
5656

5757
public static WebFormInstance createInstance(
58-
ApiClient apiClient,
59-
String accountId,
60-
String formId
61-
) throws ApiException {
58+
ApiClient apiClient,
59+
String accountId,
60+
String formId) throws ApiException {
6261

63-
//ds-snippet-start:WebForms1Step4
64-
WebFormValues formValues = new WebFormValues();
62+
// ds-snippet-start:WebForms1Step4
63+
WebFormValues formValues = new WebFormValues();
6564

6665
formValues.putAll(Map.of(
67-
"PhoneNumber", "555-555-5555",
68-
"Yes", new String[]{ "Yes" },
69-
"Company", "Tally",
70-
"JobTitle", "Programmer Writer"
71-
));
72-
//ds-snippet-end:WebForms1Step4
73-
74-
//ds-snippet-start:WebForms1Step5
75-
FormInstanceManagementApi formManagementApi = new FormInstanceManagementApi(apiClient);
66+
"PhoneNumber", "555-555-5555",
67+
"Yes", new String[] { "Yes" },
68+
"Company", "Tally",
69+
"JobTitle", "Programmer Writer"));
70+
// ds-snippet-end:WebForms1Step4
71+
72+
// ds-snippet-start:WebForms1Step5
73+
FormInstanceManagementApi formManagementApi = new FormInstanceManagementApi(apiClient);
7674
String clientUserId = "1234-5678-abcd-ijkl";
7775

7876
CreateInstanceRequestBody options = new CreateInstanceRequestBody()
79-
.clientUserId(clientUserId)
80-
.formValues(formValues)
81-
.expirationOffset(3600);
82-
77+
.clientUserId(clientUserId)
78+
.formValues(formValues)
79+
.expirationOffset(3600);
80+
8381
return formManagementApi.createInstance(accountId, formId, options);
84-
//ds-snippet-end:WebForms1Step5
82+
// ds-snippet-end:WebForms1Step5
8583
}
8684

8785
public static EnvelopeTemplate prepareEnvelopeTemplate(String templateName, String documentPdf) throws IOException {
8886
Document document = EnvelopeHelpers.createDocumentFromFile(
8987
documentPdf,
9088
"World_Wide_Web_Form",
91-
"1"
92-
);
93-
89+
"1");
90+
9491
Signer signer = new Signer()
95-
.roleName("signer")
96-
.recipientId("1")
97-
.routingOrder("1");
92+
.roleName("signer")
93+
.recipientId("1")
94+
.routingOrder("1");
9895

9996
signer.tabs(new Tabs()
100-
.checkboxTabs(List.of(
101-
new Checkbox()
102-
.documentId("1")
103-
.tabLabel("Yes")
104-
.anchorString("/SMS/")
105-
.anchorUnits("pixels")
106-
.anchorXOffset("0")
107-
.anchorYOffset("0")
108-
))
109-
.signHereTabs(List.of(
110-
new SignHere()
111-
.documentId("1")
112-
.tabLabel("Signature")
113-
.anchorString("/SignHere/")
114-
.anchorUnits("pixels")
115-
.anchorXOffset("20")
116-
.anchorYOffset("10")
117-
))
118-
.textTabs(List.of(
119-
new Text()
120-
.documentId("1")
121-
.tabLabel("FullName")
122-
.anchorString("/FullName/")
123-
.anchorUnits("pixels")
124-
.anchorXOffset("0")
125-
.anchorYOffset("0"),
126-
new Text()
127-
.documentId("1")
128-
.tabLabel("PhoneNumber")
129-
.anchorString("/PhoneNumber/")
130-
.anchorUnits("pixels")
131-
.anchorXOffset("0")
132-
.anchorYOffset("0"),
133-
new Text()
134-
.documentId("1")
135-
.tabLabel("Company")
136-
.anchorString("/Company/")
137-
.anchorUnits("pixels")
138-
.anchorXOffset("0")
139-
.anchorYOffset("0"),
140-
new Text()
141-
.documentId("1")
142-
.tabLabel("JobTitle")
143-
.anchorString("/Title/")
144-
.anchorUnits("pixels")
145-
.anchorXOffset("0")
146-
.anchorYOffset("0")
147-
))
148-
.dateSignedTabs(List.of(
149-
new DateSigned()
150-
.documentId("1")
151-
.tabLabel("DateSigned")
152-
.anchorString("/Date/")
153-
.anchorUnits("pixels")
154-
.anchorXOffset("0")
155-
.anchorYOffset("0")
156-
))
157-
);
97+
.checkboxTabs(List.of(
98+
new Checkbox()
99+
.documentId("1")
100+
.tabLabel("Yes")
101+
.anchorString("/SMS/")
102+
.anchorUnits("pixels")
103+
.anchorXOffset("0")
104+
.anchorYOffset("0")))
105+
.signHereTabs(List.of(
106+
new SignHere()
107+
.documentId("1")
108+
.tabLabel("Signature")
109+
.anchorString("/SignHere/")
110+
.anchorUnits("pixels")
111+
.anchorXOffset("20")
112+
.anchorYOffset("10")))
113+
.textTabs(List.of(
114+
new Text()
115+
.documentId("1")
116+
.tabLabel("FullName")
117+
.anchorString("/FullName/")
118+
.anchorUnits("pixels")
119+
.anchorXOffset("0")
120+
.anchorYOffset("0"),
121+
new Text()
122+
.documentId("1")
123+
.tabLabel("PhoneNumber")
124+
.anchorString("/PhoneNumber/")
125+
.anchorUnits("pixels")
126+
.anchorXOffset("0")
127+
.anchorYOffset("0"),
128+
new Text()
129+
.documentId("1")
130+
.tabLabel("Company")
131+
.anchorString("/Company/")
132+
.anchorUnits("pixels")
133+
.anchorXOffset("0")
134+
.anchorYOffset("0"),
135+
new Text()
136+
.documentId("1")
137+
.tabLabel("JobTitle")
138+
.anchorString("/Title/")
139+
.anchorUnits("pixels")
140+
.anchorXOffset("0")
141+
.anchorYOffset("0")))
142+
.dateSignedTabs(List.of(
143+
new DateSigned()
144+
.documentId("1")
145+
.tabLabel("DateSigned")
146+
.anchorString("/Date/")
147+
.anchorUnits("pixels")
148+
.anchorXOffset("0")
149+
.anchorYOffset("0"))));
158150

159151
Recipients recipients = new Recipients()
160-
.signers(List.of(signer));
161-
152+
.signers(List.of(signer));
153+
162154
return new EnvelopeTemplate()
163-
.description("Example template created via the API")
164-
.name(templateName)
165-
.shared("false")
166-
.documents(List.of(document))
167-
.emailSubject("Please sign this document")
168-
.recipients(recipients)
169-
.status("created");
155+
.description("Example template created via the API")
156+
.name(templateName)
157+
.shared("false")
158+
.documents(List.of(document))
159+
.emailSubject("Please sign this document")
160+
.recipients(recipients)
161+
.status("created");
170162
}
171163
}

0 commit comments

Comments
 (0)