Package com.dbxml.labrador.broker

Examples of com.dbxml.labrador.broker.BrokerContext


      try {
         Key key = new Key(docName);

         // Let's see if we can interact with Labrador's HTTP Server for REST calls
         BrokerContext context = Broker.getInstance().getBrokerContext();
         Handler handler = context.getHandler();
         Endpoint endpoint = context.getRequestEndpoint();

         boolean restHandler = handler instanceof RESTHandler;
         boolean httpEndpoint = endpoint instanceof HTTPServerBase;

         if ( restHandler && httpEndpoint ) {
            Request request = context.getRequest();
            Response response = context.getResponse();

            RecordMetaData meta = col.getRecordMetaData(tx, key);
            if ( meta != null ) {
               Long lm = (Long)meta.getValue(RecordMetaData.MODIFIED);
               if ( lm != null ) {
                  String lastModified = GDF.format(new Date(lm.longValue()));

                  response.setHeader(HTTP.HEADER_CACHE_CONTROL, HTTP.VALUE_CACHE);
                  response.setHeader(HTTP.HEADER_LAST_MODIFIED, lastModified);

                  String since = request.getHeader(HTTP.HEADER_IF_MODIFIED_SINCE);
                  if ( since != null && since.length() > 0 && since.equals(lastModified) )
                     throw new HTTPException(HTTP.CODE_NOT_MODIFIED, HTTP.STATUS_NOT_MODIFIED);
               }
            }
         }

         if ( type == Container.TYPE_DOCUMENT ) {
            if ( restHandler )
               context.setProperty(RESTHandler.CONTENT_TYPE, Headers.TYPE_TEXT_XML);

            String doc = DTSMHelper.tableToText(col.getDocument(tx, docName));
            return UTF8.toUTF8(doc);
         }
         else
View Full Code Here


   public Document get() throws DBException {
      Document doc = DOMHelper.newDocument();

      // Let's see if we can interact with Labrador's HTTP Server for REST calls
      BrokerContext context = Broker.getInstance().getBrokerContext();
      Handler handler = context.getHandler();
      boolean restHandler = handler instanceof RESTHandler;

      if ( restHandler ) {
         StringBuffer sb = new StringBuffer();
         sb.append("type=\"text/xsl\" href=\"");
View Full Code Here

               sessionID = new ID(value);
            }
         }
      }

      BrokerContext brokerContext = Broker.getInstance().getBrokerContext();
      ID instanceID = brokerContext.getId();
      SessionContext context = getClosestContext(instanceID);

      boolean setCookie = sessionID == null || context.expires;

      if ( sessionID == null )
         sessionID = new ID();

      if ( setCookie ) {
         StringBuffer sb = new StringBuffer();
         sb.append(COOKIE_NAME);
         sb.append('=');
         sb.append(sessionID);
         sb.append("; Path=");
         sb.append(context.path);

         if ( context.expires ) {
            // Expire when the Session times out
            sb.append("; Max-Age=");
            sb.append(Integer.toString(context.timeout));
         }
         else {
            // Expire in a few years (like not expiring at all)
            sb.append("; Expires=");
            sb.append(getExpiration());
         }

         response.setHeader(HTTP.HEADER_SET_COOKIE, sb.toString());
      }

      brokerContext.setProperty(PROP_SESSION_ID, sessionID);
      brokerContext.setProperty(PROP_SESSION_CONTEXT, context);

      return true;
   }
View Full Code Here

      }
      return info;
   }

   static Session getCurrentSession() throws SessionException {
      BrokerContext brokerContext = Broker.getInstance().getBrokerContext();

      ID sessionID = (ID)brokerContext.getProperty(PROP_SESSION_ID);
      SessionContext context = (SessionContext)brokerContext.getProperty(PROP_SESSION_CONTEXT);

      if ( sessionID != null ) {
         Map sessions = context.sessions;
         Session session = (Session)sessions.get(sessionID);
         if ( session == null ) {
View Full Code Here

         else
            throw new RequestException("No method name was set");
      }
      instance.setMethodName(methodName);

      BrokerContext context = Broker.getInstance().getBrokerContext();
      Discovery disc = context.getDiscovery();
      String[] paramNames = disc.listParams(methodName);
      boolean postUsed = false;

      for ( int i = 0; paramNames != null && i < paramNames.length; i++ ) {
         boolean array = disc.isParamArray(methodName, i);
         String[] vals = request.getValues(paramNames[i]);

         if ( vals != null ) {
            if ( array )
               instance.setParameter(i, vals);
            else
               instance.setParameter(i, vals[0]);
         }
         else if ( !postUsed && request.hasContent() ) {
            // If there is POST data, and this parameter is a String, Document,
            // or byte array, then let us assume that the POST data is intended
            // for this one.
            try {
               InputStream is = request.getInputStream();
               ByteArrayOutputStream bos = new ByteArrayOutputStream(BUFFER_SIZE);

               byte[] buffer = new byte[BUFFER_SIZE];
               int size;
               do {
                  size = is.read(buffer);
                  if ( size > 0 )
                     bos.write(buffer, 0, size);
               }
               while ( size != -1 );

               instance.setParameter(i, bos.toByteArray());
               postUsed = true;
            }
            catch ( IOException e ) {
               /** @todo Hmmm */
            }
         }
         else
            instance.setParameter(i, null);
      }

      // Initially set the MIME type in case we get an HTTP 403 Exception
      ID id = context.getId();

      String contentType = getMimeType(id);
      if ( contentType != null )
         response.setHeader(HTTP.HEADER_CONTENT_TYPE, contentType);

      Object o = instance.invoke();

      Object type = context.getProperty(CONTENT_TYPE);
      if ( type == null )
         type = instance.getProperty(CONTENT_TYPE+'_'+methodName);

      if ( type instanceof String ) {
         contentType = (String)type;
View Full Code Here

         catch ( Exception e ) {
            // The nulls will catch it
         }

         if ( user != null && pass != null && isAuthValid(user, pass) ) {
            BrokerContext ctx = Broker.getInstance().getBrokerContext();
            ctx.setProperty(PROP_USER, user);
            ctx.setProperty(PROP_PASS, pass);
            return true;
         }
         else
            throw new AuthException(HTTP.VALUE_BASIC, getAuthRealm(request));
      }
View Full Code Here

   }

   public byte[] get() throws Exception {
      try {
         // Let's see if we can interact with Labrador's HTTP Server for REST calls
         BrokerContext context = Broker.getInstance().getBrokerContext();
         Handler handler = context.getHandler();
         Endpoint endpoint = context.getRequestEndpoint();

         URL url = new URL(uri);
         URLConnection conn = url.openConnection();
         conn.setUseCaches(true);
         conn.setDoInput(true);
         conn.setDoOutput(false);

         String modSince = null;
         String lastMod = null;

         boolean browserClient = handler instanceof RESTHandler && endpoint instanceof HTTPServerBase;

         // Yank some request headers from the client and pass them through
         if ( browserClient ) {
            Request request = context.getRequest();

            for ( int i = 0; i < PASSTHRU_REQUEST_HEADERS.length; i++ ) {
               String key = PASSTHRU_REQUEST_HEADERS[i];
               String value = request.getHeader(key);
               if ( value != null && value.length() > 0 ) {
                  if ( key == HTTP.HEADER_IF_MODIFIED_SINCE )
                     modSince = value;
                  else
                     conn.setRequestProperty(key, value);
               }
            }
         }

         // Set or override Headers
         Iterator iter = headers.keySet().iterator();
         while ( iter.hasNext() ) {
            String key = (String)iter.next();
            String value = (String)headers.get(key);
            conn.setRequestProperty(key, value);
         }

         // Connect to the server and make the request
         conn.connect();

         // Yank some reponse headers from the server and pass them through
         if ( browserClient ) {
            Response response = context.getResponse();

            for ( int i = 0; i < PASSTHRU_RESPONSE_HEADERS.length; i++ ) {
               String key = PASSTHRU_RESPONSE_HEADERS[i];
               String value = conn.getHeaderField(key);
               if ( value != null && value.length() > 0 ) {
View Full Code Here

TOP

Related Classes of com.dbxml.labrador.broker.BrokerContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.