예제 #1
0
public final class Hexs {

  private static final PlatformLogger LOGGER = PlatformLogger.getLogger(Hexs.class);

  private Hexs() {
    throw new Error("Utility classes should not instantiated!");
  }

  // FIXME this method may be error
  public static String stringToHexString(String strPart) {
    return String.valueOf(Hex.encodeHex(strPart.getBytes(Charset.defaultCharset())));
  }

  public static String encode(String str) {
    return String.valueOf(Hex.encodeHex(str.getBytes(Charset.defaultCharset()), false));
  }

  public static String decode(String bytes) {
    try {
      return new String(Hex.decodeHex(bytes.toCharArray()), Charset.defaultCharset());
    } catch (DecoderException e) {
      LOGGER.error("Decode String error:{}", e);
      return StringUtils.EMPTY;
    }
  }

  public static byte[] hexString2Bytes(String src) {
    try {
      return Hex.decodeHex(src.toCharArray());
    } catch (DecoderException e) {
      LOGGER.error("Decode byte[] error:{}", e);
      return ArrayUtils.EMPTY_BYTE_ARRAY;
    }
  }
}
예제 #2
0
 public static byte[] hexString2Bytes(String src) {
   try {
     return Hex.decodeHex(src.toCharArray());
   } catch (DecoderException e) {
     LOGGER.error("Decode byte[] error:{}", e);
     return ArrayUtils.EMPTY_BYTE_ARRAY;
   }
 }
예제 #3
0
 public static String decode(String bytes) {
   try {
     return new String(Hex.decodeHex(bytes.toCharArray()), Charset.defaultCharset());
   } catch (DecoderException e) {
     LOGGER.error("Decode String error:{}", e);
     return StringUtils.EMPTY;
   }
 }
 @Override
 public void dispatchMessage(String messageBody) {
   if (messageBody != null) {
     logger.info("dispatch: " + messageBody);
     try {
       ActiveMQTextMessage message = new ActiveMQTextMessage();
       message.setText(messageBody);
       String destination = getDestination(messageBody);
       logger.info("destination:" + destination);
       if (destination != null) {
         pooledSessionProducer.sendQueue(destination, message);
       }
     } catch (MessageNotWriteableException e) {
       logger.error(e.getMessage());
       e
           .printStackTrace(); // To change body of catch statement use File | Settings | File
                               // Templates.
     }
   }
 }
/**
 * Created with IntelliJ IDEA. User: John Date: 14-3-25 Time: 下午1:42 To change this template use
 * File | Settings | File Templates.
 */
@Component
@Path("/authority")
public class AuthorityServiceWeb {

  private static PlatformLogger logger = PlatformLogger.getLogger(AuthorityServiceWeb.class);

  @Autowired AuthorityService authorityService;

  @Autowired PowerService powerService;

  @Autowired AuthorityPowerService authorityPowerService;

  @Autowired UserService userService;

  @Autowired UserAuthorityService userAuthorityService;

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/add")
  @POST
  public String add(
      @FormParam("name") String name,
      @FormParam("description") String description,
      @FormParam("status") String status,
      @FormParam("resource") String resource) {
    /*if(name==null || name.trim().equals("") || description==null || description.trim().equals("") || status==null || status.trim().equals("")){
        return JsonResultUtils.getCodeAndMesByString(JsonResultUtils.Code.ERROR.getCode(), "参数不能为空!");
    }*/
    long existAuthorityId;
    try {
      existAuthorityId = authorityService.getIdByName(name);

    } catch (Exception ex) {
      existAuthorityId = 0;
    }
    if (existAuthorityId == 0) {
      Authority authority = new Authority();
      authority.setName(name);
      authority.setDescription(description);
      authority.setStatus(Integer.parseInt(status));
      authorityService.add(authority);
      long currentAuthorityId = authorityService.getIdByName(name);
      String[] resourceArray = resource.split(";");
      for (int i = 0; i < resourceArray.length; i++) {
        long powerId = powerService.getIdByResource(resourceArray[i]);
        AuthorityPower authorityPower = new AuthorityPower();
        authorityPower.setAuthorityId(currentAuthorityId);
        authorityPower.setPowerId(powerId);
        authorityPower.setPowerResource(resourceArray[i]);
        authorityPower.setAuthorityName(name);
        authorityPowerService.add(authorityPower);
      }
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.SUCCESS);
    } else {
      return JsonResultUtils.getObjectResultByStringAsDefault("fail", JsonResultUtils.Code.ERROR);
    }
  }

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/update")
  @POST
  public String update(@FormParam("jsonString") String jsonString) {
    SubAuthority subAuthority =
        JsonMapper.buildNonDefaultMapper().fromJson(jsonString, SubAuthority.class);
    long authorityId = subAuthority.getId();
    String authorityName = subAuthority.getName();
    String authorityDescription = subAuthority.getDescription();
    int authorityStatus = subAuthority.getStatus();
    String resource = subAuthority.getResource();
    Authority authority = new Authority();
    authority.setId(authorityId);
    authority.setName(authorityName);
    authority.setDescription(authorityDescription);
    authority.setStatus(authorityStatus);
    int deleted = authorityPowerService.deleteByAuthorityName(authorityName);
    if (deleted >= 0) {
      int result = authorityService.update(authority);
      String[] resourceArray = resource.split(";");
      for (int i = 0; i < resourceArray.length; i++) {
        Long powId = powerService.getIdByResource(resourceArray[i]);
        AuthorityPower authorityPower = new AuthorityPower();
        authorityPower.setAuthorityId(authorityId);
        authorityPower.setPowerId(powId);
        authorityPower.setPowerResource(resourceArray[i]);
        authorityPower.setAuthorityName(authorityName);
        authorityPowerService.add(authorityPower);
      }
      if (result > 0) {
        return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.SUCCESS);
      } else {
        return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.ERROR);
      }
    } else {
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.ERROR);
    }
  }

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/delete")
  @POST
  public String delete(@FormParam("jsonString") String jsonString) {
    Authority authority = JsonMapper.buildNonDefaultMapper().fromJson(jsonString, Authority.class);
    String authorityName = authority.getName();
    int numDeleted = authorityPowerService.deleteByAuthorityName(authorityName);
    int result = authorityService.delete(authority);
    List<UserAuthority> userAuthorityList = userAuthorityService.findByAuthorityName(authorityName);
    if (userAuthorityList.size() > 0) {
      for (UserAuthority ua : userAuthorityList) {
        String userName = ua.getUserName();
        User user = userService.findByName(userName);
        String role = user.getRole();
        String[] roles = role.split(";");
        String[] newRoles = new String[roles.length - 1];
        int temp = 0;
        for (int i = 0; i < roles.length; i++) {
          if (!roles[i].equals(authorityName)) {
            newRoles[temp] = roles[i] + ";";
            temp++;
          }
        }
        String roles2 = "";
        String nr;
        for (int i = 0; i < newRoles.length; i++) {
          roles2 += newRoles[i];
        }
        if (roles2.equals("")) {
          nr = "";
        } else {
          nr = roles2.substring(0, roles2.length() - 1);
        }
        user.setRole(nr);
        userService.update(user);
        userAuthorityService.delete(ua);
      }
    }
    if ((result > 0) && (numDeleted >= 0)) {
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.SUCCESS);
    } else {
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.ERROR);
    }
  }

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/list")
  @GET
  public String list() {
    List<Authority> list = authorityService.list();
    List<SubAuthority> listNew = new ArrayList<SubAuthority>();
    // List<AuthorityPower> authorityPowerList = authorityPowerService.getAuthorityPowerList();
    for (Authority a : list) {
      SubAuthority subAuthority = new SubAuthority();
      String authorityName = a.getName();
      subAuthority.setId(a.getId());
      subAuthority.setName(authorityName);
      subAuthority.setDescription(a.getDescription());
      subAuthority.setStatus(a.getStatus());
      List<String> resourceList = authorityPowerService.getResourcesByAuthorityName(authorityName);
      String resources = "";
      for (String s : resourceList) {
        String s2 = s + ";";
        resources += s2;
        System.out.println(">>>>>>>>" + s);
      }
      System.out.println("<<<<<<<<" + resources);
      String r1;
      if (resources.equals("")) {
        r1 = "";
      } else {
        r1 = resources.substring(0, resources.length() - 1);
      }
      System.out.println(">>>>>>>>" + r1);
      // String r2 = "\'"+r1+"\'";
      /*String resource="";
      int length = authorityPowerList.size();
      for(int i = 0;i<length;i++){
          String authorityName = authorityPowerList.get(i).getAuthorityName();
          if(authorityName.equals(a.getName())){
              String s = "\'"+authorityPowerList.get(i).getPowerResource()+"\'";
              resource +=s ;
          }
      } */
      subAuthority.setResource(r1);
      listNew.add(subAuthority);
    }
    return JsonResultUtils.getObjectResultByStringAsDefault(listNew, JsonResultUtils.Code.SUCCESS);
  }

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/show")
  @GET
  public String show() {
    List<Authority> list = authorityService.list();
    return JsonResultUtils.getObjectResultByStringAsDefault(list, JsonResultUtils.Code.SUCCESS);
  }
}
/**
 * Created with IntelliJ IDEA. User: xiaozhujun Date: 15-4-28 Time: 下午1:49 To change this template
 * use File | Settings | File Templates.
 */
public class MinaMessageDispatcherImpl implements MinaMessageDispatcher {
  public static final PlatformLogger logger =
      PlatformLogger.getLogger(MinaMessageDispatcherImpl.class);

  // private static final String destination =
  // FundamentalConfigProvider.get("message.queue.destination");
  private HashMap<String, String> destinationMap;

  //    @Autowired
  //    private PooledMessageProducer pooledMessageProducer;
  @Autowired private PooledSessionProducer pooledSessionProducer;

  @Autowired(required = false)
  private ExceptionResolver exceptionResolver;

  @Override
  public void dispatchMessage(String messageBody) {
    if (messageBody != null) {
      logger.info("dispatch: " + messageBody);
      try {
        ActiveMQTextMessage message = new ActiveMQTextMessage();
        message.setText(messageBody);
        String destination = getDestination(messageBody);
        logger.info("destination:" + destination);
        if (destination != null) {
          pooledSessionProducer.sendQueue(destination, message);
        }
      } catch (MessageNotWriteableException e) {
        logger.error(e.getMessage());
        e
            .printStackTrace(); // To change body of catch statement use File | Settings | File
                                // Templates.
      }
    }
  }

  public void iniDestinationMap() {
    if (destinationMap != null) return;
    String destinationConfig = FundamentalConfigProvider.get("message.queue.destination.config");
    destinationMap = JsonMapper.buildNonDefaultMapper().fromJson(destinationConfig, HashMap.class);
  }

  public static void main(String[] args) {

    String test = "{\"app\":1,\"command\":1,sensors:[" + "{sensorNum:\"";
    System.out.println(test.indexOf("app\":"));
  }

  public String getDestination(String message) {
    int startIndex = message.indexOf("app\":");
    int endIndex = message.indexOf(",", startIndex);
    String app = "";
    if (endIndex > startIndex) {
      app = message.substring(startIndex + 5, endIndex);
      if (destinationMap == null) {
        iniDestinationMap();
      }
      if (destinationMap != null) {
        return destinationMap.get(app);
      }
    }
    return null;
  }

  @Override
  public void exceptionProcess() {
    if (exceptionResolver != null) {
      exceptionResolver.resolve();
    }
    /*
    String sensorNum = redisConnector.get("sensorNum");
    Long appId=sensorService.getAppIdBySNum(sensorNum);
    System.out.println(sensorNum);
    String collectorNum = collectorService.getCollectNumberBySensorNumber(sensorNum);
    List<String> sensorNumbers = sensorService.getSensorNumByCNum(collectorNum);
    for (int i=0; i<sensorNumbers.size(); i++) {
        String number = sensorNumbers.get(i).toString();
        logger.info("jjjjjjjjjjjjjjjjjj " + number);
        wsMessageDispatcher.dispatchMessage("{sensors:[{sensorNum:'" + sensorNumbers.get(i) + "',dataType:'Route',time:'"+new Date().toString()+"',data:[],id:" + sensorService.getSensorId(sensorNum,1) +
                ",appId:" + appId + ",meanVariance:0,MaxValue:0,MinValue:0,warnCount:'暂无数据',collectorNum:'" + collectorNum + "',lastCommunicateTime:'"+redisConnector.get("sensor:{"+sensorNum+"}:lastDate")+"',"+"isConnected:'"+"false"+"'"+"}]}");
    }
    collectorService.updateTimeByNumber(redisConnector.get("sensor:{"+sensorNum+"}:collector"),redisConnector.get("sensor:{"+sensorNum+"}:lastDate"));
    collectorService.updateStatusByNumber(redisConnector.get("sensor:{"+sensorNum+"}:collector"),"离线或异常");
    **/
  }
}
예제 #7
0
/**
 * Created with IntelliJ IDEA. User: sunhui Date: 14-5-12 Time: 上午9:33 To change this template use
 * File | Settings | File Templates.
 */
@Component
@Path("/app")
public class AppServiceWeb {
  private static PlatformLogger logger = PlatformLogger.getLogger(AppServiceWeb.class);

  @Autowired private AppService appService;

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/add")
  @POST
  public String add(
      @FormParam("name") String name,
      @FormParam("description") String description /*,@FormParam("status") String status*/) {
    if (name == null
        || name.trim().equals("")
        || description == null
        || description.trim().equals("") /* ||status==null|| status.trim().equals("")*/) {
      return JsonResultUtils.getCodeAndMesByString(JsonResultUtils.Code.ERROR.getCode(), "参数不能为空!");
    }
    long id;
    try {
      id = appService.getIdByName(name);
    } catch (Exception ex) {
      id = 0;
    }
    if (id == 0) {
      Date now = new Date();
      // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      // String createtime = dateFormat.format( now );

      App app = new App();
      app.setName(name);
      app.setDescription(description);
      app.setStatus("启用");
      app.setCreatetime(now);

      appService.add(app);
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.SUCCESS);
    } else {
      return JsonResultUtils.getCodeAndMesByString(JsonResultUtils.Code.ERROR.getCode(), "企业名已存在!");
    }
  }

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/list")
  @GET
  public String list() {

    List<App> list = appService.list();

    return JsonResultUtils.getObjectResultByStringAsDefault(list, JsonResultUtils.Code.SUCCESS);
  }

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/delete")
  @POST
  public String delete(@FormParam("jsonString") String jsonString) {
    App app = JsonMapper.buildNonDefaultMapper().fromJson(jsonString, App.class);

    int result = appService.delete(app);
    if (result > 0) {
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.SUCCESS);
    } else {
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.ERROR);
    }
  }

  @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
  @Path("/update")
  @POST
  public String update(@FormParam("jsonString") String jsonString) {
    App app = JsonMapper.buildNonDefaultMapper().fromJson(jsonString, App.class);
    Date now = new Date();
    app.setCreatetime(now);
    int result = appService.update(app);
    if (result > 0) {
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.SUCCESS);
    } else {
      return JsonResultUtils.getCodeAndMesByStringAsDefault(JsonResultUtils.Code.ERROR);
    }
  }
}