POST /rest/api/2/issue/{issue}/attachments HTTP/1.1
Host: {our.jira.host}
X-Atlassian-Token: nocheck
Authorization: Basic {basic authorization string}
Content-Type: multipart/form-data; boundary={our_boundary}
Content-Length: <calculated length>
--{our_boundary}
Content-Disposition: form-data; name="file"; filename="filename.ext"
Content-Type: <Content-Type header here>
(data in binary)
--{our_boundary}--
*&---------------------------------------------------------------------*
*& Report ZTMP_TEST_JIRA_UPL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTMP_TEST_JIRA_UPL.
SELECTION-SCREEN BEGIN OF BLOCK BL0 WITH FRAME TITLE TEXT-T00.
SELECTION-SCREEN BEGIN OF BLOCK BL1 WITH FRAME TITLE TEXT-T01.
PARAMETERS: p_unam TYPE string LOWER CASE,
p_pass TYPE string LOWER CASE.
SELECTION-SCREEN END OF BLOCK BL1.
SELECTION-SCREEN BEGIN OF BLOCK BL2 WITH FRAME TITLE TEXT-T02.
PARAMETERS: p_tick TYPE string,
p_file TYPE string LOWER CASE.
SELECTION-SCREEN END OF BLOCK BL2.
SELECTION-SCREEN END OF BLOCK Bl0.
AT SELECTION-SCREEN OUTPUT.
"have a little decency and hide password field
LOOP AT SCREEN.
IF screen-name = 'P_PASS'.
screen-invisible = 1.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
"file selection help
DATA: gv_rc TYPE i,
gt_file_table TYPE TABLE OF file_table.
cl_gui_frontend_services=>file_open_dialog(
CHANGING
file_table = gt_file_table
rc = gv_rc
EXCEPTIONS
others = 1
).
IF gt_file_table IS NOT INITIAL.
p_file = gt_file_table[ 1 ]-filename.
ENDIF.
START-OF-SELECTION.
IF p_file IS NOT INITIAL.
IF p_unam IS NOT INITIAL AND p_pass IS NOT INITIAL.
"upload file
DATA: gv_length TYPE i,
gv_string TYPE xstring,
gt_datatab TYPE TABLE OF x255.
cl_gui_frontend_services=>gui_upload(
EXPORTING
filename = p_file
filetype = 'BIN'
IMPORTING
filelength = gv_length
CHANGING
data_tab = gt_datatab
EXCEPTIONS
others = 1
).
IF sy-subrc = 0.
"get mime type from file
DATA: gv_mimetype TYPE SKWF_MIME.
CALL FUNCTION 'SKWF_MIMETYPE_OF_FILE_GET'
EXPORTING
filename = CONV SKWF_FILNM( p_file )
IMPORTING
mimetype = gv_mimetype.
"encode binary data to base64
"part 1
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = gv_length
IMPORTING
buffer = gv_string
tables
binary_tab = gt_datatab
EXCEPTIONS
failed = 1
.
IF sy-subrc <> 0.
"hammer time
ENDIF.
"and part 2
DATA: gv_base64 TYPE string.
gv_length = xstrlen( gv_string ).
CALL FUNCTION 'SSFC_BASE64_ENCODE'
EXPORTING
bindata = gv_string
binleng = gv_length
IMPORTING
b64data = gv_base64.
"authorization encoding
DATA gv_auth_xstring TYPE xstring.
DATA(gv_auth_string) = p_unam && `:` && p_pass.
DATA: gv_auth_string_base64 TYPE string.
gv_auth_xstring = gv_auth_string.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = gv_auth_string
IMPORTING
buffer = gv_auth_xstring.
CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
EXPORTING
input = gv_auth_xstring
IMPORTING
output = gv_auth_string_base64.
"get filename from filepath
SPLIT p_file AT `\` INTO TABLE DATA(gt_file).
IF gt_file IS NOT INITIAL.
DATA(gv_filename) = gt_file[ lines( gt_file ) ].
ELSE.
gv_filename = sy-datum && '_file'.
ENDIF.
"call RFC fm
DATA gv_http_code TYPE string.
CALL FUNCTION 'ZFM_JIRA_ADD_ATTACHMENT_RFC' DESTINATION 'PI_RFC'
EXPORTING
iv_issue_key = p_tick
iv_authstring = gv_auth_string_base64
is_attachment = VALUE zjira_attachment_s( filename = gv_filename
mimetype = gv_mimetype
data = gv_base64 )
IMPORTING
ev_http_code = gv_http_code
EXCEPTIONS
system_failure = 1.
DATA(gv_msg) = sy-subrc && ` - sy-subrc. Has status : ` && gv_http_code.
MESSAGE gv_msg TYPE 'S'.
ENDIF.
ELSE.
MESSAGE 'Enter credentials' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ELSE.
MESSAGE 'Select file' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:jira:projects" targetNamespace="urn:jira:projects">
<xsd:element name="EmptyRequest" type="empty" />
<xsd:simpleType name="empty">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="status" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
package com.zooplus.mapping;
import java.io.*;
import com.sap.aii.mapping.api.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import java.util.Base64;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class AddAttachmentJIRAMap extends AbstractTransformation {
private static final String LINE_FEED = "\r\n";
public void transform(TransformationInput inStream, TransformationOutput outStream) throws StreamTransformationException {
AbstractTrace trace = (AbstractTrace) getTrace();
trace.addInfo("attachment mapping started");
String boundary = "--r_BWbX54zeRleg";//TODO: auto generate
String body = "";
String base64AuthString = "";
String filename = "";
String contentType = "";
String base64File = "";
String issueName = "";
//parse input stream
InputStream inputstream = inStream.getInputPayload().getInputStream();
DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
try {
//get data from input xml
docBuilder = docBuildFactory.newDocumentBuilder();
Document doc = docBuilder.parse(inputstream);
NodeList oElementsAuthString = doc.getElementsByTagName("IV_AUTHSTRING");
NodeList oElementIssueName = doc.getElementsByTagName("IV_ISSUE_KEY");
NodeList oElementFilename = doc.getElementsByTagName("FILENAME");
NodeList oElementContentType = doc.getElementsByTagName("MIMETYPE");
NodeList oElementBase64File = doc.getElementsByTagName("DATA");
base64AuthString = oElementsAuthString.item(0).getTextContent();
issueName = oElementIssueName.item(0).getTextContent();
filename = oElementFilename.item(0).getTextContent();
contentType = oElementContentType.item(0).getTextContent();
base64File = oElementBase64File.item(0).getTextContent();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
} catch (SAXException | IOException e1) {
e1.printStackTrace();
}
//set dynamic conf
String namespace = "http://sap.com/xi/XI/System/REST";
DynamicConfiguration DynConfig = inStream.getDynamicConfiguration();
DynamicConfigurationKey key = DynamicConfigurationKey.create(namespace, "ISSUE_NO");//for dynamic URL
DynConfig.put(key,issueName);
key = DynamicConfigurationKey.create(namespace, "BOUNDARY_VAL");//for header
DynConfig.put(key, "multipart/form-data; boundary=" + boundary);
key = DynamicConfigurationKey.create(namespace, "AUTH_STRING");//for authorization
DynConfig.put(key, "Basic " + base64AuthString);
//set payload
//decode base64 to byte table
byte[] decodedBytes = Base64.getDecoder().decode(base64File);
//create 1st part of a body
body = "--" + boundary;
body = this.addKey( "Content-Disposition", "form-data; name=\"file\"; filename=\"" + filename + "\"", body );
body = this.addKey( "Content-Type", contentType, body );
body = this.addLine( body );
body = this.addLine( body );
//3rd part (body end)
String body_end = LINE_FEED + "--" + boundary + "--";
//write to output stream
try {
OutputStream outputstream = outStream.getOutputPayload().getOutputStream();
outputstream.write(body.getBytes());
outputstream.write(decodedBytes);
outputstream.write(body_end.getBytes());
trace.addInfo("attachment mapping ended");
} catch (IOException e) {
throw new StreamTransformationException(e.getMessage());
}
}
private String addKey(final String key, final String value, final String body){
final String body_c = body + LINE_FEED + key + ": " + value;
return body_c;
}
private String addLine(final String body) {
return body + LINE_FEED;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
53 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |