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

📄 rtspstoredsessionservlet.java

📁 Sun公司Dream项目
💻 JAVA
字号:
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * http://www.opensource.org/licenses/cddl1.php
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * http://www.opensource.org/licenses/cddl1.php.  If 
 * applicable, add the following below this CDDL HEADER, 
 * with the fields enclosed by brackets "[]" replaced 
 * with your own identifying information: 
 * Portions Copyright [yyyy]
 * [name of copyright owner]
 */ 

/*
 * $(@)RTSPStoredSessionServlet.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
 * 
 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
 */
package com.sun.sjc.idtv.vod.server.rtspsession;



import javax.xml.soap.*;

import javax.servlet.*;

import javax.servlet.http.*;

import javax.rmi.PortableRemoteObject;

import javax.ejb.*; 



import javax.xml.transform.*;



import java.util.*;

import java.io.*;

import java.sql.*;



import com.sun.sjc.idtv.vod.shared.data.*;



public class RTSPStoredSessionServlet extends HttpServlet {



    static MessageFactory fac = null;

    static {

        try {

            fac = MessageFactory.newInstance();

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    };

    static RTSPStoredSessionEngine rsse;

    static RTSPStoredSessionEngineHome rsseHome;





    MessageFactory msgFactory;



    public void init(ServletConfig servletConfig) throws ServletException {



        super.init(servletConfig);





        javax.naming.InitialContext initContext = null; 

        Hashtable env = new java.util.Hashtable(1); 

        try { 

            initContext = new javax.naming.InitialContext(); 



            String JNDIName = "java:comp/env/ejb/RTSPStoredSessionEngine"; 

            System.out.println("Looking up: " + JNDIName); 

            Object objref = initContext.lookup(JNDIName); 

            rsseHome = (RTSPStoredSessionEngineHome)PortableRemoteObject.narrow(objref, RTSPStoredSessionEngineHome.class);

            System.out.println("Creating the rtspstoredsessionengine bean"); 

            rsse = rsseHome.create();  

	} catch (Exception e) {

	    throw new ServletException("Unable to connect to RTSPStoredSessionEngine " + e.getMessage());

	}



        try {

            // Initialize it to the default.

            msgFactory = MessageFactory.newInstance();

        } catch (SOAPException ex) {

            throw new ServletException("Unable to create message factory " + ex.getMessage());

        }

    }

    

    public void doPost( HttpServletRequest req, 

			HttpServletResponse resp)

	throws ServletException, IOException {

	try {

	    // Get all the headers from the HTTP request.

	    MimeHeaders headers = getHeaders(req);



	    // Get the body of the HTTP request.

	    InputStream is = req.getInputStream();



	    // Now internalize the contents of a HTTP request and

	    // create a SOAPMessage

	    SOAPMessage msg = msgFactory.createMessage(headers, is);



	    SOAPMessage reply = null;



	    reply = onMessage(msg, req.getRemoteAddr());



	    if (reply != null) {

                

		// Need to call saveChanges because we're going to use the

		// MimeHeaders to set HTTP response information. These

		// MimeHeaders are generated as part of the save.



		if (reply.saveRequired()) {

		    reply.saveChanges(); 

		}



		resp.setStatus(HttpServletResponse.SC_OK);



		putHeaders(reply.getMimeHeaders(), resp);

                    

		// Write out the message on the response stream.

		OutputStream os = resp.getOutputStream();

		reply.writeTo(os);

    

		os.flush();

                    

	    } else 

		resp.setStatus(HttpServletResponse.SC_NO_CONTENT);



	} catch (Exception ex) {

	    throw new ServletException("JAXM POST failed "+ex.getMessage());

	}

    }



    static MimeHeaders getHeaders(HttpServletRequest req) {



	Enumeration enum1 = req.getHeaderNames();

	MimeHeaders headers = new MimeHeaders();



	while (enum1.hasMoreElements()) {

	    String headerName = (String)enum1.nextElement();

	    String headerValue = req.getHeader(headerName);



	    StringTokenizer values = new StringTokenizer(headerValue, ",");

	    while (values.hasMoreTokens())

		headers.addHeader(headerName, values.nextToken().trim());

	}



	return headers;

    }



    static void putHeaders(MimeHeaders headers, 

			   HttpServletResponse res) {



	Iterator it = headers.getAllHeaders();

	while (it.hasNext()) {

	    MimeHeader header = (MimeHeader)it.next();



	    String[] values = headers.getHeader(header.getName());

	    if (values.length == 1)

		res.setHeader(header.getName(), header.getValue());

	    else {

		StringBuffer concat = new StringBuffer();

		int i = 0;

		while (i < values.length) {

		    if (i != 0)

			concat.append(',');

		    concat.append(values[i++]);

		}



		res.setHeader(header.getName(), concat.toString());

	    }

	}

    }



    // This is the application code for handling the message.



    public SOAPMessage onMessage(SOAPMessage message, String requestIp) {

	SOAPMessage reply = null;

        try {

            //retrieve orderID from message received

	    SOAPBody receivedSB = message.getSOAPPart().getEnvelope().getBody();

        

	    Iterator receivedIt = receivedSB.getChildElements();

        SOAPBodyElement receivedSBE = (SOAPBodyElement)receivedIt.next();

        System.out.println("onMessage(): SOAPBodyElement.getElementName() = "+receivedSBE.getElementName().getLocalName());

        

        String requestType = receivedSBE.getElementName().getLocalName();

	    

        Iterator receivedIt2 = receivedSBE.getChildElements();

	    

        reply = fac.createMessage();

    	SOAPPart sp = reply.getSOAPPart();

	    SOAPEnvelope env = sp.getEnvelope();

        env.getHeader().detachNode();

    	SOAPBody sb = env.getBody();

            

        Name name = null;

        SOAPElement soapElement = null;

        String temp = null;



        if(requestType.equals("retrieveRTSPSession-request")) {

            String userId=null;

            while(receivedIt2.hasNext()) {

	            soapElement = (SOAPElement)receivedIt2.next();

                name = soapElement.getElementName();

                temp = name.getLocalName();

                if(temp.equals("userId")) {

                    userId = soapElement.getValue();

                }

            }

            if(userId == null) {

                // Error Message  (throw an Exception? or return null? or return

                // error soap message?)    

            }

            RTSPSessionObject[] rtspSessionObjects = rsse.getStoredRTSPSessions(userId);

            

            Name newBodyName = env.createName("retrieveRTSPSession-reply", "", "http://vod-vas");

            SOAPBodyElement replySBE = sb.addBodyElement(newBodyName);

            

            Name assetName = null;

            

            SOAPElement userIdElement = null;

            SOAPElement movieIdElement = null;

            SOAPElement timestElement = null;

            SOAPElement smpteElement = null;

            

            for(int i=0; i<rtspSessionObjects.length; i++) {

                assetName = env.createName("Asset "+i);

                SOAPElement asset = replySBE.addChildElement(assetName);

                userIdElement = asset.addChildElement("userId");

                userIdElement.addTextNode(rtspSessionObjects[i].getUserId());

                movieIdElement = asset.addChildElement("movieIdName");

                movieIdElement.addTextNode(rtspSessionObjects[i].getMovieId());

                timestElement = asset.addChildElement("timestName");

                timestElement.addTextNode(rtspSessionObjects[i].getTimestamp().toString());

                smpteElement = asset.addChildElement("smpteName");

                smpteElement.addTextNode(rtspSessionObjects[i].getSmpte());

            }

        } else if(requestType.equals("storeRTSPSession-request")) {

            RTSPSessionObject rtspSessionObject = new RTSPSessionObject();

            while(receivedIt2.hasNext()) {

                soapElement = (SOAPElement)receivedIt2.next();

                name = soapElement.getElementName();

                temp = name.getLocalName();

                if(temp.equals("userId")) {

                    rtspSessionObject.setUserId(soapElement.getValue());

                } else if (temp.equals("movieId")) {

                    rtspSessionObject.setMovieId(soapElement.getValue());

                } else if (temp.equals("timest")) {

                    rtspSessionObject.setTimestamp(Timestamp.valueOf(soapElement.getValue()));

                } else if (temp.equals("smpte")) {

                    rtspSessionObject.setSmpte(soapElement.getValue());

                }

            }

            Name newBodyName = env.createName("storeRTSPSession-reply","","http://vod-vas");

            SOAPBodyElement replySBE = sb.addBodyElement(newBodyName);

            Name statusName = env.createName("status");

            soapElement = replySBE.addChildElement(statusName);

            try {

                rsse.storeRTSPSession(rtspSessionObject);

                soapElement.addTextNode("ok");

            } catch (SOAPException soapEx) {

                soapEx.printStackTrace();    

            } catch (Exception e) {

                e.printStackTrace();

                soapElement.addTextNode("failed");

            }

        }

        reply.saveChanges();

    	} catch (Exception ex) {

	        ex.printStackTrace();

	    }

	return reply;

    }

}



⌨️ 快捷键说明

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