我正在尝试查找从一个缺陷到另一个副本的复制附件的解决方案。
我尝试使用以下方法,但获取附件正在返回
com.atlassian.jira.rpc.soap.beans.RemoteAttachment
的类型,我没有看到任何StringBuffer
或任何实际上有base64编码对象的对象,其中包含Attachment本身。
add Attachments不接受此DataType。 http://mulesoft.github.io/jira-connector/mule/jira-config.html#add-base64-encoded-attachments-to-issue
如何将附件从一个缺陷成功上传到另一个缺陷?
<flow name="jira-attachments-POC">
<http:listener config-ref="HTTP_Listener_Configuration" path="/attachments" doc:name="HTTP"/>
<jira:get-attachments-from-issue config-ref="Jira" issueKey="MRT-75" doc:name="Jira"/>
<set-session-variable variableName="attachementsPayload" value="#[payload]" doc:name="attachementsPayload"/>
<set-session-variable variableName="issueAttachmentNames" value="#[payload[0].attachmentNames]" doc:name="issueAttachmentNames"/>
<set-session-variable variableName="fileAttachmentsArray" value="*" doc:name="Session Variable fileAttachments"/>
<set-session-variable variableName="fileName" value="#[payload[0].filename]" doc:name="fileName"/>
<set-session-variable variableName="fileId" value="#[payload[0].id]" doc:name="fileId"/>
<set-session-variable variableName="requestPath" value="secure/attachment/#[sessionVars.fileId]/#[sessionVars.fileName]" doc:name="requestPath"/>
<component doc:name="Java" class="JIRAAttachmentObjectTransformer"/>
<jira:add-base64-encoded-attachments-to-issue config-ref="Jira" issueKey="MRT-655" doc:name="Jira">
<jira:file-names ref="#[sessionVars.fileNamesArray]"/>
<jira:base64-encoded-attachment-data ref="#[sessionVars.fileAttachmentsArray]"/>
</jira:add-base64-encoded-attachments-to-issue>
<logger level="INFO" doc:name="Logger" message="{payload=#[payload]}"/>
</flow>
变压器代码。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.codec.binary.Base64;
import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.transport.PropertyScope;
import org.mule.transformer.AbstractMessageTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JIRAAttachmentObjectTransformer implements org.mule.api.lifecycle.Callable {
private static Logger logger = LoggerFactory
.getLogger(JIRAAttachmentObjectTransformer.class);
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
MuleMessage message = eventContext.getMessage();
String reqPath = eventContext.getMessage().getProperty("requestPath", PropertyScope.SESSION);
System.out.println("RequestPath:->"+reqPath);
try{
URL fileConnectPath = new URL("http://xxxx.com:8443/"+reqPath);
URLConnection uc = fileConnectPath.openConnection();
String userpass = "xxxxx" + ":" + "xxxxx";
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
uc.setRequestProperty ("Authorization", basicAuth);
//URLConnection myURLConnection = myURL.openConnection();
//myURLConnection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
uc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
String byteArrayStr= new String(Base64.encodeBase64(inputLine.getBytes()));
String strArray[] = new String[1];
strArray[0] = byteArrayStr;
eventContext.getMessage().setProperty("fileAttachmentsArray", strArray, PropertyScope.SESSION);
String fileName = eventContext.getMessage().getProperty("fileName", PropertyScope.SESSION);
String fileNamesArray[] = new String[1];
fileNamesArray[0]= fileName;
eventContext.getMessage().setProperty("fileNamesArray", fileNamesArray , PropertyScope.SESSION);
System.out.println("Successfully Added to message.");
}catch(Exception e){
e.printStackTrace();
}
return message;
}
}
为
设置的有效负载类型是什么以下是我现在得到的错误..
RequestPath: - &GT;安全/附接/ 160026/2015年4月3日 12_03_17-Clipboard.png java.net.SocketException:意外结束 来自服务器的文件 sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:772)at at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:769)at at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1324) 在 JIRAAttachmentObjectTransformer.onCall(JIRAAttachmentObjectTransformer.java:38) 在 org.mule.model.resolvers.CallableEntryPointResolver.invoke(CallableEntryPointResolver.java:46) 在 org.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:36) 在 org.mule.component.DefaultComponentLifecycleAdapter.invoke(DefaultComponentLifecycleAdapter.java:339) 在 org.mule.component.AbstractJavaComponent.invokeComponentInstance(AbstractJavaComponent.java:82) 在 org.mule.component.AbstractJavaComponent.doInvoke(AbstractJavaComponent.java:73) 在 org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:125) 在 org.mule.component.AbstractComponent.access $ 000(AbstractComponent.java:59) 在 org.mule.component.AbstractComponent $ 1 $ 1.process(AbstractComponent.java:241) 在 org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24) 在 org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:58) 在 org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44) 在 org.mule.processor.chain.DefaultMessageProcessorChain.doProcess(DefaultMessageProcessorChain.java:94) 在 org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:69) 在 org.mule.processor.chain.InterceptingChainLifecycleWrapper.doProcess(InterceptingChainLifecycleWrapper.java:50) 在 org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:69) 在 org.mule.processor.chain.InterceptingChainLifecycleWrapper.access $ 001(InterceptingChainLifecycleWrapper.java:22) 在 org.mule.processor.chain.InterceptingChainLifecycleWrapper $ 1.process(InterceptingChainLifecycleWrapper.java:66) 在 org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24) 在 org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:58) 在 org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44) 在 org.mule.processor.chain.InterceptingChainLifecycleWrapper.process(InterceptingChainLifecycleWrapper.java:61) 在 org.mule.component.AbstractComponent.process(AbstractComponent.java:159) 在 org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24) 在 org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:58) 在 org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44) 在 org.mule.processor.chain.DefaultMessageProcessorChain.doProcess(DefaultMessageProcessorChain.java:94) 在 org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:69) 在 org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24) 在 org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44) 在
答案 0 :(得分:0)
我通过以下方法解决了这个问题。
骡流将附件从一个缺陷移动到另一个缺陷。按照以下步骤在我的本地Java环境中添加HTTPS安装证书,以便我可以使用https://特定的JIRA URL
http://magicmonster.com/kb/prg/java/ssl/pkix_path_building_failed.html
<flow name="jira-attachments-POC">
<http:listener config-ref="HTTP_Listener_Configuration" path="/attachments" doc:name="HTTP"/>
<jira:get-attachments-from-issue config-ref="Jira" issueKey="#[message.inboundProperties.sourceIssueId]" doc:name="Jira"/>
<set-session-variable variableName="attachementsPayload" value="#[payload]" doc:name="attachementsPayload"/>
<foreach doc:name="For Each">
<set-session-variable variableName="issueAttachmentNames" value="#[payload.attachmentNames]" doc:name="issueAttachmentNames"/>
<set-session-variable variableName="fileAttachmentsArray" value="*" doc:name="Session Variable fileAttachments"/>
<set-session-variable variableName="fileName" value="#[payload.filename]" doc:name="fileName"/>
<set-session-variable variableName="fileSize" value="#[payload.filesize]" doc:name="fileSize"/>
<set-session-variable variableName="fileId" value="#[payload.id]" doc:name="fileId"/>
<set-session-variable variableName="srcIssueKey" value="#[message.inboundProperties.sourceIssueId]" doc:name="srcIssueKey"/>
<set-session-variable variableName="destIssueKey" value="#[message.inboundProperties.targetIssueId]" doc:name="destIssueKey"/>
<set-session-variable variableName="uploadAttachementBaseURL" value="https://www.jiraurl.com:8443/rest/api/2/" doc:name="uploadAttachementBaseURL"/>
<set-session-variable variableName="readAttachmentBaseURL" value="https://www.jirawebsite.com/secure/attachment/" doc:name="readAttachmentBaseURL"/>
<component class="JIRAAttachmentObjectTransformer" doc:name="Java"/>
</foreach>
<set-payload value="{"message":"Sucessfully Attachments Migrated."}" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger" />
</flow>
复制附件和移动到另一个缺陷的Java代码。
import java.io.File; import java.io.IOException;
import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.mule.api.MuleEventContext; import org.mule.api.MuleMessage; import org.mule.api.transport.PropertyScope; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import de.schlichtherle.io.FileOutputStream;
public class JIRAAttachmentObjectTransformer implements org.mule.api.lifecycle.Callable { private static Logger logger = LoggerFactory .getLogger(JIRAAttachmentObjectTransformer.class);
@Override public Object onCall(MuleEventContext eventContext) throws Exception { MuleMessage message = eventContext.getMessage();
String userName = "xxx"; String password = "xxxxxx";
String sourceBaseURL = eventContext.getMessage().getProperty("readAttachmentBaseURL", PropertyScope.SESSION); String sourceIssueFileId = eventContext.getMessage().getProperty("fileId", PropertyScope.SESSION); String sourceIssueKey = eventContext.getMessage().getProperty("srcIssueKey", PropertyScope.SESSION); String sourceFileName = eventContext.getMessage().getProperty("fileName", PropertyScope.SESSION);
if( ( sourceIssueFileId != null || sourceIssueKey.trim().length() > 0 ) && ( sourceFileName != null || sourceFileName.trim().length() > 0 ) ) { String targetIssueKey = eventContext.getMessage().getProperty("destIssueKey", PropertyScope.SESSION); String targetBaseURL = eventContext.getMessage().getProperty("uploadAttachementBaseURL", PropertyScope.SESSION);
String targetURL = targetBaseURL+"issue/"+sourceIssueKey+"/attachments"; String sourceURL = sourceBaseURL+ sourceIssueFileId +"/" ;
String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64((userName+":"+password).getBytes()));
File targetFileName = getAttachmentFromIssue( sourceURL , sourceFileName , auth);
System.out.println("Get Attachment Status:->" + targetFileName.toString() );
boolean status = addAttachmentToIssue(targetBaseURL, auth, targetIssueKey, targetFileName);
System.out.println("Upload file to JIRA #" + targetIssueKey + " Status "+ status); }else{ System.out.println("No Attachments Found to Migrate Ticket" );
}
return message;
}
private File getAttachmentFromIssue(String contentURI, String fullfilename, String auth) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault(); File fileName = null;
try { HttpGet httpget = new HttpGet(contentURI); logger.info("Source URI:->"+contentURI); httpget.setHeader("Authorization", "Basic " + auth); System.out.println("Executing request " + httpget.getURI()); CloseableHttpResponse response = httpclient.execute(httpget);
int status = response.getStatusLine().getStatusCode(); logger.info("Get Attachment:->" + fullfilename +"::Http Response code:->"+ status ); if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
if (entity.isStreaming()) {
byte data[] = EntityUtils.toByteArray(entity);
fileName = new File(fullfilename);
FileOutputStream fout = new FileOutputStream(fileName);
fout.write(data);
fout.close();
}
logger.info("Successfully file Copied."); } } finally {
httpclient.close();
}
return fileName;
}
private boolean addAttachmentToIssue(String baseJiraURL, String auth, String issueKey, File fileName) throws IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(baseJiraURL+"issue/"+issueKey+"/attachments"); logger.info("Target URL:->"+baseJiraURL+"issue/"+issueKey+"/attachments");
httppost.setHeader("X-Atlassian-Token", "nocheck");
httppost.setHeader("Authorization", "Basic "+auth);
FileBody fileBody = new FileBody(fileName);
HttpEntity entity = MultipartEntityBuilder.create().addPart("file", fileBody).build();
httppost.setEntity(entity);
String mess = "executing request " + httppost.getRequestLine();
logger.info(mess);
CloseableHttpResponse response;
try {
response = httpclient.execute(httppost);
logger.info("JIRA Ticket ID:"+issueKey+"::HttResponse code:->" + response.getStatusLine().getStatusCode());
} finally {
httpclient.close();
}
if(response.getStatusLine().getStatusCode() == 200)
return true;
else
return false;
}
}