2017-03-16 1 views
0

私は有効として、以下の3つのパラメータを持つURLを取り、残りのエンドポイントがあります。 レグノ、ホストID、場所 レグノ、domid、場所 レグノ、プロバイダ休憩検索パラメータの検証

これらの組み合わせ以外のものを無効です。 私はこの

if (!StringUtils.isEmpty(criteria.getRegno())) { 
if ((!StringUtils.isEmpty(criteria.getHostid()) || !StringUtils.isEmpty(criteria.getDomId())) && !StringUtils.isEmpty(criteria.getLocation())) { 
criteria.setSearchType(GET_HOST_SEARCH_TYPE); 
} else if (!StringUtils.isEmpty(criteria.getProvider()) && (StringUtils.isEmpty(criteria.getLocation()) && StringUtils.isEmpty(criteria.getHostid) && StringUtils.isEmpty(criteria.getDomId()))) { 
criteria.setSearchType(GET_PROVIDER_SEARCH_TYPE); 
} else { 
throw new BadRequestException("Either Provider, Location, Hostid or domid is missing"); 
} 
} else { 
throw new BadRequestException("Regno is missing"); 
} 

をチェックするバリデータメソッドを持っている私は、私がもしelse文の多くを使用していたという事実を好みません。これを行うより読みやすい方法があれば、お気軽にお手伝いください。

+0

パラメータ –

+0

localhost /を検索して、有効な完全なURL文字列のサンプルを与える場所=インド&レグノ= 12532&ホストID = GDY-101:次のような – Raskill

答えて

1

あなたは、次のアプローチを試みることがあり、それはあなたがこのメソッドにURL文字列を渡す必要が

public String detectSearchType(String url) throws BadRequestException{ 
     final String condition1 = "(?=.*location)(?=.*(?:hostid|domid))"; 
     final String condition2 = "(?=.*provider)(?!.*hostid)(?!.*domid)(?!.*location)"; 

     if(!url.contains("regno=")) 
      throw new BadRequestException("Regno is missing"); 
     else if(Pattern.compile(condition1).matcher(url).find()) 
      return "GET_HOST_SEARCH_TYPE"; 
     else if(Pattern.compile(condition2).matcher(url).find()) 
      return "GET_PROVIDER_SEARCH_TYPE"; 
     else 
      throw new BadRequestException("Either Provider, Location, Hostid or domid is missing"); 

    } 

..大幅に他の場合の必要性を軽減します。 ?

detectSearchType("localhost/search?location=india&regno=12532&hostid=gdy-101"); 
detectSearchType("localhost/search?location=india&regno=12532&domid=gdy-101"); 
detectSearchType("localhost/search?regno=12532&provider=mrt"); 
detectSearchType("localhost/search?regno=12532&provider=mrt&host=abul"); 
detectSearchType("localhost/abc?regno=1&hostid=2"); 
関連する問題