@SuppressWarnings("unchecked")
  @Override
  public WOResponse handleRequest(WORequest request) {
    WOApplication application = WOApplication.application();
    application.awake();
    try {
      WOContext context = application.createContextForRequest(request);
      WOResponse response = application.createResponseInContext(context);

      Object output;
      try {
        String inputString = request.contentString();
        JSONObject input = new JSONObject(inputString);
        String wosid = request.cookieValueForKey("wosid");
        if (wosid == null) {
          ERXMutableURL url = new ERXMutableURL();
          url.setQueryParameters(request.queryString());
          wosid = url.queryParameter("wosid");
          if (wosid == null && input.has("wosid")) {
            wosid = input.getString("wosid");
          }
        }
        context._setRequestSessionID(wosid);
        WOSession session = null;
        if (context._requestSessionID() != null) {
          session = WOApplication.application().restoreSessionWithID(wosid, context);
        }
        if (session != null) {
          session.awake();
        }
        try {
          JSONComponentCallback componentCallback = null;

          ERXDynamicURL url = new ERXDynamicURL(request._uriDecomposed());
          String requestHandlerPath = url.requestHandlerPath();
          JSONRPCBridge jsonBridge;
          if (requestHandlerPath != null && requestHandlerPath.length() > 0) {
            String componentNameAndInstance = requestHandlerPath;
            String componentInstance;
            String componentName;
            int slashIndex = componentNameAndInstance.indexOf('/');
            if (slashIndex == -1) {
              componentName = componentNameAndInstance;
              componentInstance = null;
            } else {
              componentName = componentNameAndInstance.substring(0, slashIndex);
              componentInstance = componentNameAndInstance.substring(slashIndex + 1);
            }

            if (session == null) {
              session = context.session();
            }

            String bridgesKey =
                (componentInstance == null) ? "_JSONGlobalBridges" : "_JSONInstanceBridges";
            Map<String, JSONRPCBridge> componentBridges =
                (Map<String, JSONRPCBridge>) session.objectForKey(bridgesKey);
            if (componentBridges == null) {
              int limit =
                  ERXProperties.intForKeyWithDefault(
                      (componentInstance == null)
                          ? "er.ajax.json.globalBacktrackCacheSize"
                          : "er.ajax.json.backtrackCacheSize",
                      WOApplication.application().pageCacheSize());
              componentBridges = new LRUMap<String, JSONRPCBridge>(limit);
              session.setObjectForKey(componentBridges, bridgesKey);
            }
            jsonBridge = componentBridges.get(componentNameAndInstance);
            if (jsonBridge == null) {
              Class componentClass = _NSUtilities.classWithName(componentName);
              JSONComponent component;
              if (JSONComponent.class.isAssignableFrom(componentClass)) {
                component =
                    (JSONComponent)
                        _NSUtilities.instantiateObject(
                            componentClass,
                            new Class[] {WOContext.class},
                            new Object[] {context},
                            true,
                            false);
              } else {
                throw new SecurityException(
                    "There is no JSON component named '" + componentName + "'.");
              }
              jsonBridge =
                  createBridgeForComponent(
                      component, componentName, componentInstance, componentBridges);
            }

            componentCallback = new JSONComponentCallback(context);
            jsonBridge.registerCallback(componentCallback, WOContext.class);
          } else {
            jsonBridge = _sharedBridge;
          }

          try {
            output = jsonBridge.call(new Object[] {request, response, context}, input);
          } finally {
            if (componentCallback != null) {
              jsonBridge.unregisterCallback(componentCallback, WOContext.class);
            }
          }

          if (context._session() != null) {
            WOSession contextSession = context._session();
            // If this is a new session, then we have to force it to be a cookie session
            if (wosid == null) {
              boolean storesIDsInCookies = contextSession.storesIDsInCookies();
              try {
                contextSession.setStoresIDsInCookies(true);
                contextSession._appendCookieToResponse(response);
              } finally {
                contextSession.setStoresIDsInCookies(storesIDsInCookies);
              }
            } else {
              contextSession._appendCookieToResponse(response);
            }
          }
          if (output != null) {
            response.appendContentString(output.toString());
          }

          if (response != null) {
            response._finalizeInContext(context);
            response.disableClientCaching();
          }
        } finally {
          try {
            if (session != null) {
              session.sleep();
            }
          } finally {
            if (context._session() != null) {
              WOApplication.application().saveSessionForContext(context);
            }
          }
        }
      } catch (NoSuchElementException e) {
        e.printStackTrace();
        output =
            new JSONRPCResult(
                JSONRPCResult.CODE_ERR_NOMETHOD, null, JSONRPCResult.MSG_ERR_NOMETHOD);
      } catch (JSONException e) {
        e.printStackTrace();
        output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
      } catch (Throwable t) {
        t.printStackTrace();
        output = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, t.getMessage());
      }

      return response;
    } finally {
      application.sleep();
    }
  }
Beispiel #2
0
 public static WOActionResults exportJournalZPU(
     NSArray journal, WOContext context, String filename) {
   WOSession ses = context.session();
   if (journal == null || journal.count() == 0) {
     WOResponse response = WOApplication.application().createResponseInContext(context);
     response.appendContentString(
         (String) ses.valueForKeyPath("strings.RujelCurriculum_Curriculum.Tabel.noData"));
     response.setHeader("application/octet-stream", "Content-Type");
     response.setHeader("attachment; filename=\"noData.txt\"", "Content-Disposition");
     return response;
   }
   Export export = new ExportCSV(context, filename);
   export.beginRow();
   export.addValue(ses.valueForKeyPath("strings.Reusables_Strings.dataTypes.Date"));
   export.addValue(ses.valueForKeyPath("strings.RujelCurriculum_Curriculum.OrigTeacher"));
   export.addValue(ses.valueForKeyPath("strings.RujelInterfaces_Names.EduCycle.subject"));
   export.addValue(ses.valueForKeyPath("strings.RujelInterfaces_Names.EduGroup.this"));
   export.addValue(ses.valueForKeyPath("strings.RujelCurriculum_Curriculum.Reason.Reason"));
   export.addValue(ses.valueForKeyPath("strings.RujelCurriculum_Curriculum.Reason.verification"));
   export.addValue(
       ses.valueForKeyPath("strings.RujelCurriculum_Curriculum.Substitute.Substitutor"));
   export.addValue(ses.valueForKeyPath("strings.RujelInterfaces_Names.EduCycle.subject"));
   export.addValue(ses.valueForKeyPath("strings.RujelCurriculum_Curriculum.Substitute.factor"));
   Enumeration enu = journal.objectEnumerator();
   StringBuilder buf = new StringBuilder();
   while (enu.hasMoreElements()) {
     NSDictionary dict = (NSDictionary) enu.nextElement();
     export.beginRow();
     export.addValue(MyUtility.dateFormat().format(dict.valueForKey("date")));
     EduCourse course = (EduCourse) dict.valueForKey("minusCourse");
     if (course != null) {
       Teacher teacher = (Teacher) dict.valueForKey("minusTeacher");
       if (teacher != null) export.addValue(Person.Utility.fullName(teacher, true, 2, 1, 1));
       else export.addValue(ses.valueForKeyPath("strings.RujelBase_Base.vacant"));
       if (course.comment() == null) {
         export.addValue(course.cycle().subject());
       } else {
         buf.delete(0, buf.length());
         buf.append(course.cycle().subject());
         buf.append(' ').append('(').append(course.comment()).append(')');
         export.addValue(buf.toString());
       }
     } else {
       export.addValue(null);
       export.addValue(null);
     }
     if (dict.valueForKey("eduGroup") != null)
       export.addValue(dict.valueForKeyPath("eduGroup.name"));
     else export.addValue(dict.valueForKey("grade"));
     export.addValue(dict.valueForKeyPath("reason.title"));
     export.addValue(dict.valueForKeyPath("reason.verification"));
     course = (EduCourse) dict.valueForKey("plusCourse");
     if (course != null) {
       Teacher teacher = (Teacher) dict.valueForKey("plusTeacher");
       export.addValue(Person.Utility.fullName(teacher, true, 2, 1, 1));
       if (course.comment() == null) {
         export.addValue(course.cycle().subject());
       } else {
         buf.delete(0, buf.length());
         buf.append(course.cycle().subject());
         buf.append(' ').append('(').append(course.comment()).append(')');
         export.addValue(buf.toString());
       }
     } else {
       export.addValue(null);
       export.addValue(null);
     }
     export.addValue(dict.valueForKey("value"));
   }
   return export;
 }