ActionEnter.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.baidu.ueditor;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import com.baidu.ueditor.define.ActionMap;
  5. import com.baidu.ueditor.define.AppInfo;
  6. import com.baidu.ueditor.define.BaseState;
  7. import com.baidu.ueditor.define.State;
  8. import com.baidu.ueditor.hunter.FileManager;
  9. import com.baidu.ueditor.hunter.ImageHunter;
  10. import com.baidu.ueditor.upload.Uploader;
  11. public class ActionEnter {
  12. private HttpServletRequest request = null;
  13. private String rootPath = null;
  14. private String contextPath = null;
  15. private String actionType = null;
  16. private ConfigManager configManager = null;
  17. public ActionEnter ( HttpServletRequest request, String rootPath ) {
  18. this.request = request;
  19. this.rootPath = rootPath;
  20. this.actionType = request.getParameter( "action" );
  21. this.contextPath = request.getContextPath();
  22. this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );
  23. this.configManager.updateConfig();
  24. }
  25. public String exec () {
  26. String callbackName = this.request.getParameter("callback");
  27. if ( callbackName != null ) {
  28. if ( !validCallbackName( callbackName ) ) {
  29. return new BaseState( false, AppInfo.ILLEGAL ).toJSONString();
  30. }
  31. return callbackName+"("+this.invoke()+");";
  32. } else {
  33. return this.invoke();
  34. }
  35. }
  36. public String invoke() {
  37. if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
  38. return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
  39. }
  40. if ( this.configManager == null || !this.configManager.valid() ) {
  41. return new BaseState( false, AppInfo.CONFIG_ERROR ).toJSONString();
  42. }
  43. State state = null;
  44. int actionCode = ActionMap.getType( this.actionType );
  45. Map<String, Object> conf = null;
  46. try{
  47. switch ( actionCode ) {
  48. case ActionMap.CONFIG:
  49. return this.configManager.getAllConfig().toString();
  50. case ActionMap.UPLOAD_IMAGE:
  51. case ActionMap.UPLOAD_SCRAWL:
  52. case ActionMap.UPLOAD_VIDEO:
  53. case ActionMap.UPLOAD_FILE:
  54. conf = this.configManager.getConfig( actionCode );
  55. state = new Uploader( request, conf ).doExec();
  56. break;
  57. case ActionMap.CATCH_IMAGE:
  58. conf = configManager.getConfig( actionCode );
  59. String[] list = this.request.getParameterValues( (String)conf.get( "fieldName" ) );
  60. state = new ImageHunter( conf ).capture( list );
  61. break;
  62. case ActionMap.LIST_IMAGE:
  63. case ActionMap.LIST_FILE:
  64. conf = configManager.getConfig( actionCode );
  65. int start = this.getStartIndex();
  66. state = new FileManager( conf ).listFile( start );
  67. break;
  68. }
  69. }catch(Exception e){
  70. e.printStackTrace();
  71. }
  72. return state.toJSONString();
  73. }
  74. public int getStartIndex () {
  75. String start = this.request.getParameter( "start" );
  76. try {
  77. return Integer.parseInt( start );
  78. } catch ( Exception e ) {
  79. return 0;
  80. }
  81. }
  82. /**
  83. * callback参数验证
  84. */
  85. public boolean validCallbackName ( String name ) {
  86. if ( name.matches( "^[a-zA-Z_]+[\\w0-9_]*$" ) ) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. }