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

📄 acsservlet.java

📁 openacs source for CPE wan management
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * ACSServlet.java
 *
 * Created on Sekmadienis, 2007, Rugs雑o 9, 14.39
 */
package org.openacs;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.Calendar;
import java.util.Collection;
import java.util.logging.*;
import java.util.NoSuchElementException;

import java.util.Set;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.soap.*;


import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import org.openacs.message.*;
import org.openacs.utils.*;

/**
 *
 * @author Administrator
 * @version
 */
public class ACSServlet extends HttpServlet {

    private class MyAuthenticator extends Authenticator {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            String ui = this.getRequestingURL().getUserInfo();
            System.out.println ("MyAuthenticator: ui="+ui);
            if (ui == null || ui.equals("")) return super.getPasswordAuthentication();
            String up[] = ui.split(":");
            char[] pc = new char[up[1].length()];
            up[1].getChars(0, up[1].length(), pc, 0);
            PasswordAuthentication pa = new PasswordAuthentication(up[0], pc);
            return pa;
        }
    }

    private class xmlFilterInputStream extends InputStream {

        /** Creates a new instance of xmlFilterInputStream */
        public xmlFilterInputStream(InputStream is, int l) {
            //      System.out.println("Stream length is "+l);
            len = l;
            istream = is;
        }

        public int read() throws IOException {
            if (lastchar == '>' && lvl == 0) {
                //        System.err.println ("return EOF");
                return -1;
            }
            int l = lastchar;
            if (nextchar != -1) {
                lastchar = nextchar;
                nextchar = -1;
            } else {
                lastchar = istream.read();
            }
            if (l == '<') {
                if (lastchar == '/') {
                    lvl--;
                } else {
                    lvl++;
                }
            }
            //           System.err.println ("return char="+(char)lastchar+" lvl="+lvl);
            //System.err.print ((char)lastchar);
            len--;
            return lastchar;
        }
        private InputStream istream;
        private int lvl;
        private int lastchar;
        private int len;
        private int nextchar;

        public boolean next() throws IOException {
            while ((nextchar = istream.read()) != -1) {
                if (!Character.isWhitespace(nextchar)) {
                    break;
                }
            }
            //        System.out.println ("Next char is "+nextchar);
            lvl = 0;
            lastchar = 0;
            return (nextchar != -1);
        }
    }

    private boolean authenticate(HostsLocal host, HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
        System.out.println ("host="+host +" request="+request+" response="+response);
        System.out.println ("user="+host.getUsername());
        System.out.println ("pass="+host.getPassword());
        System.out.println ("authtype="+host.getAuthtype());
         */
        return HttpAuthentication.Authenticate(host.getUsername(), host.getPassword(), host.getAuthtype(), request, response);
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * 
     * 
     * 
     * @param request ACSServlet request
     * @param response ACSServlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger logger = Logger.getLogger(getClass().getName());
        ServletContext ctx = getServletContext();
        boolean autoCreateCPE = Boolean.parseBoolean(ctx.getInitParameter("org.openacs.AutoCreateCPE"));

//        OutputStream out = response.getOutputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SOAPMessage soapMsg = null;
//        FileInputStream fi = new FileInputStream("c:\\tmp\\tst.xml");
//      xmlFilterInputStream f = new xmlFilterInputStream(fi, fi.available());
        xmlFilterInputStream f = new xmlFilterInputStream(request.getInputStream(), request.getContentLength());
        MessageFactory mf;
        //SOAPFactory spf;
        try {
            mf = MessageFactory.newInstance();
        //spf = SOAPFactory.newInstance();
        } catch (SOAPException e) {
            throw new ServletException();
        }
        response.setContentType("text/xml;charset=UTF-8");
        //response.setContentType("text/xml");
        int countEnvelopes = 0;
        HttpSession session = request.getSession();
        String oui, sn;
        Inform lastInform = (Inform) session.getAttribute("lastInform");
        /*
        int maxEnvelopes = (lastInform != null) ? lastInform.MaxEnvelopes : 2;
        if (maxEnvelopes < 2) {
        System.out.println("MaxEnvelopes are less then 2 (" + maxEnvelopes + "). Setting to 2");
        maxEnvelopes = 2;
        }
         */
        int maxEnvelopes = (lastInform != null) ? lastInform.MaxEnvelopes : MIN_MAX_ENVELOPES;
        if (maxEnvelopes < MIN_MAX_ENVELOPES) {
            System.out.println("MaxEnvelopes are less then " + MIN_MAX_ENVELOPES + " (" + maxEnvelopes + "). Setting to " + MIN_MAX_ENVELOPES);
            maxEnvelopes = 1;
        }
        // <editor-fold defaultstate="collapsed" desc="Get/create JMS producer&consumer">
//        QueueSession queuesession = null;
//        javax.jms.Queue queue = null;
        // </editor-fold>
        // <editor-fold defaultstate="expanded" desc="Process CPE requests/responses">
        HardwareModelLocal hw = (HardwareModelLocal) session.getAttribute("hardware");
        HostsLocal lasthost = (HostsLocal) session.getAttribute("host");
        while (f.next()) {
            try {
                //System.out.println("createMessage");
                soapMsg = mf.createMessage(null, f);

                String reqname = Message.getRequestName(soapMsg);
                System.out.println("Request is " + reqname);

                Message msg = null;
                try {
                    msg = (Message) Class.forName("org.openacs.message." + reqname).newInstance();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                    return;
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                    return;
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                    return;
                }

                msg.parse(soapMsg);
                logger.log(Level.FINEST, msg.toString());

                //if (msg.isFault()) { continue; }

                try {
                    // <editor-fold defaultstate="expanded" desc="Process Inform request">
                    if (reqname.equals("Inform")) {
                        lastInform = (Inform) msg;
                        session.setAttribute("lastInform", lastInform);

                        InformResponse resp = new InformResponse(lastInform.getId (), MY_MAX_ENVELOPES);
                        resp.writeTo(out);
                        countEnvelopes++;

                        maxEnvelopes = lastInform.MaxEnvelopes;
                        sn = lastInform.sn;
                        oui = lastInform.oui;
                        String url = lastInform.getURL();
                        //System.out.println("oui="+oui+", sn="+sn+", URL="+url);
                        logger.log(Level.INFO, "oui=" + oui + ", sn=" + sn + ", URL=" + url + ", hw=" + lastInform.getHardwareVersion() + ", sw=" + lastInform.getSoftwareVersion() + ", cfg=" + lastInform.getConfigVersion()+", ProvisioningCode="+lastInform.getProvisiongCode());

                        HardwareModelLocalHome hwhome = Ejb.lookupHardwareModelBean();
                        try {
                            hw = hwhome.findByOuiAndClassAndVersion(oui, lastInform.ProductClass, lastInform.getHardwareVersion());
                        } catch (FinderException e) {
                            try {
                                hw = hwhome.create(lastInform.ProductClass, lastInform.Manufacturer, oui, lastInform.ProductClass, lastInform.getHardwareVersion());
                            } catch (CreateException ex) {
                                ex.printStackTrace();
                                throw new RuntimeException();
                            }
                        }

                        // <editor-fold defaultstate="expanded" desc="Updating hwid to contain version">
                        HardwareModelLocal hwold = null;
                        try {
                            hwold = hwhome.findByOuiAndClass(oui, lastInform.ProductClass);
                        } catch (FinderException ex) {
                        }
                        // </editor-fold>

                        session.setAttribute("hardware", hw);

                        HostsLocalHome hsthome = Ejb.lookupHostsBean();
                        //System.out.println("SERVLET: looked up HostsLocalHome");
                        HostsLocal host = null;
                        // <editor-fold defaultstate="expanded" desc="Updating hwid to contain version">
                        if (hwold != null) {
                            try {
                                host = hsthome.findByHwidAndSn((Integer) hwold.getId(), sn);
                                host.setHwid((Integer)hw.getId());
                            } catch (FinderException ex) {
                            }
                        }
                        // </editor-fold>

                        try {

⌨️ 快捷键说明

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