⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendtodevice.java

📁 java xml 应用开发
💻 JAVA
字号:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.apache.commons.logging.LogFactory;
import org.cip4.jdflib.core.DocumentJDFImpl;
import org.cip4.jdflib.core.JDFDoc;
import org.cip4.jdflib.core.JDFParser;
import org.cip4.jdflib.jmf.JDFJMF;
import org.cip4.jdflib.jmf.JDFMessage;
import org.w3c.dom.Element;

import sun.rmi.runtime.Log;



/*
 * Created on 12.07.2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author MRE (Institute for Print and Media Technology)
 * History:
 * 20040903 MRE send MIME multipart/related
 */
public class SendToDevice extends JPanel implements ActionListener
{
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -4676135228882149268L;
    
    private File file;
    private ResourceBundle littleBundle;
    private JTextField urlPath;
    private JRadioButton rbJMF;
    private JRadioButton rbMIME;
//    private JButton browse;
    private INIReader iniFile;
    
    public SendToDevice(File file, ResourceBundle bundle)
    {
        super();
        this.file = file;
        this.littleBundle = bundle;
        
        setLayout(new GridLayout(3, 1, 0, 5));
        init();
        setVisible(true);
    }

    /**
     * Creates the input field for the URL of the device.
     * The behaviour of the of the input window depends on the
     * settings in the Editor.ini
     *  -if method is set to "JMF" - the message will be send
     *   as a SubmitQueueEntry with the location of the JDF
     *   as an URL
     * - if method is set to "MIME" a multipart/related message 
     *   will be send
     * - if method is set to "User" the user will be able to
     *   choose the method to be send by means of radio buttons 
     */
    private void init()
    {
        iniFile = new INIReader();
        
        final JLabel urlText = new JLabel(littleBundle.getString("setURL"));
        urlText.setVerticalAlignment(SwingConstants.BOTTOM);
        add(urlText);
        
        final JLabel urlLabel = new JLabel(littleBundle.getString("pathToURL"));

        urlPath = new JTextField(50);
        
        final Box URLBox = Box.createHorizontalBox();
        
        URLBox.add(urlLabel);
        URLBox.add(urlPath);
        URLBox.add(Box.createHorizontalStrut(5));

        // show radio buttons only if "Users Choice" method is set
        if(iniFile.getMethodSendToDevice().equals("User")){
            //RadioButtons to choose sending JDF with QueueSubmissionParams
            //and URL or as a multipart/related MIME message
            rbJMF = new JRadioButton( littleBundle.getString("sendMethodJMF") );
            rbMIME = new JRadioButton( littleBundle.getString("sendMethodMIME") );
            rbMIME.setSelected( true );
            final ButtonGroup sendMethodGroup = new ButtonGroup();
            sendMethodGroup.add( rbJMF ); sendMethodGroup.add( rbMIME );
            
            final JLabel rbLabel = new JLabel(littleBundle.getString("sendMethod"));
            
            final Box SendMethodBox = Box.createHorizontalBox();
            
            SendMethodBox.add(rbLabel);
            SendMethodBox.add(rbJMF);
            SendMethodBox.add(rbMIME);
            add(SendMethodBox);
        }
        //add boxes to the window
        add(URLBox);
    }
    
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) 
    { /**/ }

    /**
     * sendJMFJDFmime
     * sends the actual open JDF as a MIME multipart/related message 
     * to the given URL
     * 
     * @param url
     * @return
     */
    public boolean sendJMFJDFmime(URL url){
        
        boolean bSendTrue = false;
        
       // Logger.debug("sendJDF as JMF to device: " + url);
        
        if (url != null){
            try{
                // create a JMF message
                final DocumentJDFImpl dJI = new DocumentJDFImpl();
                final JDFJMF jaj = new JDFJMF(dJI, "JMF");
                
                //get the JDF
                final JDFParser p = new JDFParser();
                final JDFDoc jdfDocIn = p.parseFile (file.getAbsolutePath());
                
                // create a command message
                jaj.appendCommand(JDFMessage.EnumType.SubmitQueueEntry);
                
                //create a QueueSubmissionParams element
                final Element eQsp = dJI.createElement("QueueSubmissionParams");
                
                //create an unique CID
                final Random rand = new Random(12345);
                final Integer intrand = new Integer(rand.nextInt());
                final String sJDFCID = new String(intrand.toString());
                            
                eQsp.setAttribute("URL", sJDFCID);
                    
                // insert the QueueSubmissionParams
                final Element eCommand = jaj.getDeepElement("Command","",0);
                eCommand.appendChild(eQsp);
    
    //             Create empty properties
                final Properties props = new Properties();
    
    //             Get session
                final Session session = Session.getDefaultInstance(props, null);
                final Message mMessage = new MimeMessage(session);
                
                //body part for the JMF
                final BodyPart mMessageBodyPartJMF = new MimeBodyPart();
                
                //write JMF to body part
                // the MIME type will be replaced by the send method
                mMessageBodyPartJMF.setContent(jaj.toXML(), "text/xml");
                
    //             Create a related multi-part to combine the parts
                final MimeMultipart mpMultipart = new MimeMultipart("related");
                mpMultipart.addBodyPart(mMessageBodyPartJMF);
    
    //            create part for the JDF
                final BodyPart mMessageBodyPartJDF = new MimeBodyPart();
                
                mMessageBodyPartJDF.setHeader("Content-ID", sJDFCID);
                
                //mMessageBodyPartJDF.setContent(jdfDocIn, "application/vnd.cip4-jdf+xml");
                mMessageBodyPartJDF.setContent(jdfDocIn.toXML(), "text/xml");
                            
    //            add JDF part to multi-part
                mpMultipart.addBodyPart(mMessageBodyPartJDF);
                
                                
    //            associate multi-part with message
                mMessage.setContent(mpMultipart);
                
                bSendTrue =  send(url, mMessage);
            }
            catch (Exception e){
        //        Logger.fatal(e);
                return false;
            }
        }
      //  Logger.debug("bSendTrue: " + bSendTrue);
        return bSendTrue?true:false;
    }
    /**
     * sendJMFJDF
     * sends the actual open JDF as a JMF command 
     * to the given URL
     * 
     * @param url
     * @return
     */

    public boolean sendJMFJDF(URL url){
        
        boolean bSendTrue = false;
        
       // Logger.debug("sendJDF as JMF to device: " + url);
        
        if (url != null){
            try{
                // create a JMF message
                final DocumentJDFImpl dJI = new DocumentJDFImpl();
                final JDFJMF jaj = new JDFJMF(dJI, "JMF");
                
                //get the JDF
                final JDFParser p = new JDFParser();
                p.parseFile (file.getAbsolutePath());
                
                // create a command message
                jaj.appendCommand(JDFMessage.EnumType.SubmitQueueEntry);
                
                //create a QueueSubmissionParams element
                final Element eQsp = dJI.createElement("QueueSubmissionParams");
                
                            
                eQsp.setAttribute("URL", file.getAbsolutePath());
                    
                // insert the QueueSubmissionParams
                final Element eCommand = jaj.getDeepElement("Command","",0);
                eCommand.appendChild(eQsp);
                
                bSendTrue =  send(url, jaj.toXML());
            }
            catch (Exception e){
               // Logger.fatal(e);
                return false;
            }
        }
    //    Logger.debug("bSendTrue: " + bSendTrue);
        return bSendTrue?true:false;
    }
    
    /**
     * send
     * 
     * set the MIME type application/vnd.cip4-jdf+xml 
     * 
     * @param URL url (URL of the device)
     * @param Message mMessage (message to be sent)
     * @return
     */
    static boolean send(URL url, Message mMessage)
    {
      //  Logger.debug("send MIME message");
        
        try    {

            final OutputStream sOut = new ByteArrayOutputStream();

            mMessage.writeTo(sOut);
            String sMessage = sOut.toString();
            
            // replace the MIME type by the CIP4 MIME type
            sMessage = sMessage.replaceAll("text/xml","application/vnd.cip4-jdf+xml");
        //    Logger.debug("message to be sent: " +sMessage);
            
            // send changed message
            send(url, sMessage);

        }
            catch (Exception e){
         //       Logger.fatal(e);
                return false;
            }
            return true;
    }

    /**
     * send
     * 
     * performes the sending process
     * set the MIME type application/vnd.cip4-jdf+xml 
     * 
     * @param URL url (URL of the device)
     * @param Message mMessage (message to be sent)
     * @return
     */
    static boolean send(URL url, String sMessage)
    {
       // Logger.debug("send MIME message");
        try    {
            HttpURLConnection httpURLconnection;            
            httpURLconnection = (HttpURLConnection) url.openConnection();
            //httpURLconnection.setRequestMethod("POST");
            //httpURLconnection.setRequestProperty("Connection","close");
            httpURLconnection.setRequestProperty("Content-type", "application/vnd.cip4-jmf+xml");
            httpURLconnection.setRequestProperty("User-Agent", "Queue (JMF protocol)");
            httpURLconnection.setRequestProperty("Accept", "application/vnd.cip4-jmf+xml");
            httpURLconnection.setUseCaches(false);
            httpURLconnection.setDoOutput(true);
            httpURLconnection.setDoInput(true);
            
            final OutputStream out    = httpURLconnection.getOutputStream();
            
            // replace the MIME type by the CIP4 MIME type
            sMessage.replaceAll("text/xml","application/vnd.cip4-jdf+xml");
            
            out.write(sMessage.getBytes());
          
      //      Logger.debug(out);
        
            out.flush();
            out.close();
            
            httpURLconnection.disconnect();
        }
            catch (Exception e){
     //           Logger.fatal(e);
                return false;
            }
            return true;
    }
    
    
    /**
     * getURL
     * 
     * returns the URL of the device given by the user
     * 
     * @return URL url
     */
    
    public URL getURL() {
        // returns the URL given by the user
        URL url = null;
        try {
            url = new URL(urlPath.getText());
        } catch (MalformedURLException e) {
            //e.printStackTrace();
            return null;
        }
        return url;
    }    

    /**
     * getActiveRadioButton()
     * 
     * returns the radio button set for the send method
     * 
     * @return String activeRadioButton
     */
    public String getActiveRadioButton() {
        // returns the URL given by the user
        String activeRadioButton = new String();
        try {
            if(rbJMF.isSelected())
                activeRadioButton = littleBundle.getString("sendMethodJMF");
            else
                activeRadioButton = littleBundle.getString("sendMethodMIME");
        } catch (Exception e) {
            //e.printStackTrace();
            return null;
        }
        return activeRadioButton;
    }        
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -