📄 submitrequest.java
字号:
*/ public void setDistributionIndicator( boolean distributionIndicator ) { this.distributionIndicator = new Boolean( distributionIndicator ); } /** * Sets the subject field of this message. * <p> * The subject is encoded into the default charset of the platform on which the API * runs. It is encoded according to the protocol defined in Request For Comments (RFC)2047: * <em>MIME (Multipurpose Internet Mail Extensions) Part Three: Message Header Extensions for * Non-ASCII Text</em>.</p> * * @param subject The subject of this message. * @exception APIException If an error occurs while encoding the value. */ public void setSubject( String subject ) throws APIException { try { if( subject != null ) { this.subject = MimeUtility.encodeText( subject, "UTF-8", "B" ); } } catch( UnsupportedEncodingException uee ) { throw new APIException( "unsupported-encoding", uee.getMessage() ); } } /** * Sets the subject field of this message. * <p> * The subject is encoded into the supplied charset. It is encoded according to the protocol * defined in Request For Comments (RFC)2047: * <em>MIME (Multipurpose Internet Mail Extensions) Part Three: Message Header Extensions for * Non-ASCII Text</em>.</p> * * @param subject The subject of this message. * @param charset The charset of this message. Any charset supported by Java is a valid value. * @exception APIException If an error occurs while encoding the value. */ public void setSubject( String subject, String charset ) throws APIException { try { if( subject != null ) { this.subject = MimeUtility.encodeText( subject, charset, "B" ); } } catch( UnsupportedEncodingException uee ) { throw new APIException( "unsupported-encoding", uee.getMessage() ); } } /** * Sets the multimedia message content of this message. The parameter * can be an instance of <code>List</code> where it is expected to * contain a list of <code>Slide</code>s or <code>MultimediaContent</code> * or <code>javax.mail.MimeBodyPart</code> or <code>javax.mail.MimeMultipart</code> * * @param content The multimedia content of this message as a List of * <code>Slide</code> objects created using the composition api. * @throws IllegalArgumentException if the object passed in is not one * of the types listed above. * @throws ContentException If there is an error creating the <code>MultimediaContent</code> * object from the object passed in. * @throws IOException If there is an error getting content from the * object passed in. * @throws MessagingException If the object passed in is a JavaMail object * and there is an error processing it. * */ public void setContent( Object content ) throws ContentException, MessagingException, IOException { if( content instanceof List ) { this.content = Factory.getInstance().newMultimediaContent(); ( ( MultimediaContent ) this.content ).setSlides( ( List ) content ); } else if( content instanceof MultimediaContent ) { this.content = ( MultimediaContent ) content; } else if( content instanceof MimeBodyPart ) { this.content = content; } else if( content instanceof MimeMultipart ) { this.content = Factory.getInstance().newMultimediaContent( ( MimeMultipart ) content ); } else if( content instanceof MimeBodyPart[] ) { MimeBodyPart[] parts = ( MimeBodyPart[] ) content; MimeMultipart multipart = new MimeMultipart( "related" ); for( int i = 0; i < parts.length; i++ ) { multipart.addBodyPart( parts[ i ] ); } this.content = Factory.getInstance().newMultimediaContent( multipart ); } else { throw new IllegalArgumentException( "illegal-content-in-setcontent" ); } } /** * Protected method that the {@link Request} class uses to write this message * to an <code>OutputStream</code> object. This method is for API internal use only. * * @param outputStream The <code>OutputStream</code> object to which this object is written. * @throws APIException If the API encounters an error while composing the message from MIME * body parts or while writing the message to the output stream. * @throws SOAPException If any of the SOAP Envelope creation rules are violated. * @throws ContentException If the content processing results in an error. * */ protected void writeTo( OutputStream outputStream ) throws APIException, ContentException, SOAPException { SOAPEnvelope env = new SOAPEnvelope( null ); SOAPMethod method = new SOAPMethod( new SOAPQName( SOAPConsts.MM7SubmitReqMethodName, "mm7", mm7Namespace ), ( Vector ) null ); env.getBody().setMethod( method ); super.serialize( env ); if( recipients.isEmpty() ) { throw new APIException( "no-recipients-specified" ); } SOAPParameter recipients = method.addParameter( SOAPConsts.MM7RecipientsParameterName ); buildAddressList( recipients ); if( serviceCode != null && serviceCode.length() > 0 ) { method.addParameter( SOAPConsts.MM7ServiceCodeParameterName ) .setValue( serviceCode ); } if( linkedID != null && linkedID.length() > 0 ) { method.addParameter( SOAPConsts.MM7LinkedIDParameterName ) .setValue( linkedID ); } if( messageClass != null ) { method.addParameter( SOAPConsts.MM7MessageClassParameterName ) .setValue( messageClass.toString() ); } // TimeStamp should be here if( replyChargingSize > 0 || ( replyDeadline != null && replyDeadline.length() > 0 ) ) { SOAPParameter replyCharging = method.addParameter( SOAPConsts.MM7ReplyChargingParameterName ); if( replyChargingSize > 0 ) { replyCharging.addAttribute( SOAPConsts.MM7ReplyChargingSizeAttributeName, String.valueOf( replyChargingSize ) ); } if( replyDeadline != null && replyDeadline.length() > 0 ) { replyCharging.addAttribute( SOAPConsts.MM7ReplyDeadlineAttributeName, replyDeadline ); } } if( earliestDeliveryTime != null && earliestDeliveryTime.length() > 0 ) method.addParameter( SOAPConsts.MM7EarliestDeliveryTimeParameterName ) .setValue( earliestDeliveryTime ); if( expiry != null && expiry.length() > 0 ) method.addParameter( SOAPConsts.MM7ExpiryDateParameterName ) .setValue( expiry ); if( deliveryReport != null ) { method.addParameter( SOAPConsts.MM7DeliveryReportParameterName ) .setValue( deliveryReport.toString() ); } if( readReply != null ) { method.addParameter( SOAPConsts.MM7ReadReplyParameterName ) .setValue( readReply.toString() ); } if( priority != null ) { method.addParameter( SOAPConsts.MM7PriorityParameterName ) .setValue( priority.toString() ); } if( subject != null && subject.length() > 0 ) method.addParameter( SOAPConsts.MM7SubjectParameterName ) .setValue( subject ); if( chargedParty != null ) { method.addParameter( SOAPConsts.MM7ChargedPartyParameterName ) .setValue( chargedParty.toString() ); } if( distributionIndicator != null ) { if( mm7Namespace.equals( Namespace.REL_5_MM7_1_0.toString() ) ) { method.addParameter( SOAPConsts.MM7DistributionProtectionParameterName ) .setValue( distributionIndicator.toString() ); } else { method.addParameter( SOAPConsts.MM7DistributionIndicatorParameterName ) .setValue( distributionIndicator.toString() ); } } if( content != null ) { SOAPParameter contentParam = method.addParameter( SOAPConsts.MM7ContentParameterName ); cid = Utils.generateContentID(); contentParam.addAttribute( SOAPConsts.MM7ContentHrefAttributeName, "cid:" + cid ); } // make the mime multipart/related mm7 document String envStr = env.toString(); try { MimeMultipart swa = new MimeMultipart( "related" ); MimeBodyPart soapPart = new MimeBodyPart(); soapPart.setContent( envStr, "text/xml" ); swa.addBodyPart( soapPart ); addContent( swa, cid ); MimeMessage mess = new MimeMessage( Session.getDefaultInstance( new Properties() ) ); mess.setContent( swa ); mess.saveChanges(); String[] contentType = mess.getHeader( "content-type" ); // The default writeTo method doesnt write ContentLength header // which is required by anacapa. There is a getSize method which doesnt // seem to be able to calculate the size(returns -1). So we have to go thru this exercise // of removing all the headers, writing the message to a bytearrayoutputstream and // calculating its size Enumeration headers = mess.getAllHeaders(); int i = 0; while( headers.hasMoreElements() ) { Header header = ( Header ) headers.nextElement(); mess.removeHeader( header.getName() ); } ByteArrayOutputStream baos = new ByteArrayOutputStream( 4096 ); mess.writeTo( baos ); outputStream.write( ("Content-Length: " + (baos.size()-2) + "\r\n" ).getBytes() ); outputStream.write( ("Content-Type: " + contentType[0] + "\r\n" ).getBytes() ); baos.writeTo( outputStream ); if( logger.isDebugEnabled() ) { logger.debug( "Content-Length: " + (baos.size()-2) + "\r\n" ); logger.debug( "Content-Type: " + contentType[0] + "\r\n" ); logger.debug( baos.toString() ); } } catch( MessagingException me ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( me.getMessage(), me ); } throw new APIException( "messaging-exception-composing", me.getMessage() ); } catch( IOException ie ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( ie.getMessage(), ie ); } throw new APIException( "io-exception-writing-to-socket", ie.getMessage() ); } } private void addContent( MimeMultipart swa, String cid ) throws MessagingException, ContentException { if( content != null ) { // content object can either be a MimeBodyPart or MultimediaContent only // this is governed by setContent if( content instanceof MimeBodyPart ) { MimeBodyPart mm = ( MimeBodyPart ) content; mm.setHeader( "Content-ID", "<" + cid + ">" ); swa.addBodyPart( mm ); } else if( content instanceof MultimediaContent ) { // even if it is a MultimediaContent object, the getContent from // MultimediaContent could return a MimeBodyPart or a MimeMultipart // depending on how it was created. MultimediaContent mmContent = ( MultimediaContent ) content; Object contentFromMC = mmContent.getContent(); if( contentFromMC instanceof MimeBodyPart ) { MimeBodyPart mm = ( MimeBodyPart ) contentFromMC; mm.setHeader( "Content-ID", "<" + cid + ">" ); swa.addBodyPart( mm ); } else if( contentFromMC instanceof MimeMultipart ) { MimeMultipart multipartContent = ( MimeMultipart ) mmContent.getContent(); MimeBodyPart mm = new MimeBodyPart(); mm.setHeader( "Content-ID", "<" + cid + ">" ); mm.setContent( multipartContent ); String start = getSmilContentID( multipartContent ); if( start != null ) { String contentType = multipartContent.getContentType(); contentType = contentType + ";\r\n\tstart=\"" + start + "\"" + ";\r\n\ttype=\"application/smil\""; mm.setHeader( "Content-Type", contentType ); } swa.addBodyPart( mm ); } } } } private void buildAddressList( SOAPParameter rcpts ) throws SOAPException { for( int i = 0; i < recipients.size(); i++ ) { Recipient recipient = ( Recipient ) recipients.get( i ); SOAPParameter type = rcpts.getParameter( recipient.getType().toString() ); if( type == null ) { type = new SOAPParameter( recipient.getType().toString() ); rcpts.addParameter( type ); } SOAPParameter addressType = new SOAPParameter( recipient.getAddressType().toString() ); addressType.setValue( recipient.getAddress() ); type.addParameter( addressType ); } } private static String getSmilContentID( MimeBodyPart[] content ) throws MessagingException { for( int i = 0; i < content.length; i++ ) { if( content[i].getDataHandler().getContentType().startsWith( "application/smil" ) ) { return content[i].getContentID(); } } return null; } private static String getSmilContentID( MimeMultipart multipart ) throws MessagingException { for( int i = 0; i < multipart.getCount(); i++ ) { MimeBodyPart part = ( MimeBodyPart ) multipart.getBodyPart( i ); if( part.getContentType().startsWith( "application/smil" ) ) { return part.getContentID(); } } return null; } private static final Logger logger = Logger.getLogger( SubmitRequest.class ); private Vector recipients; private String serviceCode; private String linkedID; private MessageClass messageClass; private String date; private int replyChargingSize; private String replyDeadline; private String earliestDeliveryTime; private String expiry; private Boolean deliveryReport; private Boolean readReply; private Priority priority; private String subject; private ChargedParty chargedParty; private Boolean distributionIndicator; private Object content; private String cid;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -