Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
git: Make git server supports i18n
Browse files Browse the repository at this point in the history
Make Yobi as a git server says in user's preferred language. Git
2.4.0-rc0 or more is required.
  • Loading branch information
Yi EungJun committed Apr 6, 2015
1 parent e4acd2e commit 1e9b9ee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
43 changes: 35 additions & 8 deletions app/controllers/GitApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import models.Project;
import models.enumeration.Operation;

import com.github.zafarkhaja.semver.Version;
import play.api.i18n.Lang;
import play.i18n.Messages;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.With;
import playRepository.PlayRepository;
import playRepository.RepositoryService;
import utils.AccessControl;
import utils.BasicAuthAction;
import utils.Config;

public class GitApp extends Controller {

Expand All @@ -55,6 +59,26 @@ private static boolean isAllowed(Project project, String service) throws

}

/**
* Checks whether the Git client allows Content-Type has a charset.
*
* Charset is allowed since Git 2.1.0.
*
* @param userAgent
* @return
*/
private static boolean isCharsetAllowed(String userAgent) {
try {
String version = Config.semverize(userAgent.substring(userAgent.indexOf('/') + 1));
if (Version.valueOf(version).greaterThanOrEqualTo(Version.forIntegers(2, 1, 0))) {
return true;
}
} catch (Exception e) {
return false;
}
return false;
}

public static Result service(String ownerName, String projectName, String service,
boolean isAdvertise) throws IOException, UnsupportedOperationException,
ServletException {
Expand All @@ -78,14 +102,17 @@ public static Result service(String ownerName, String projectName, String servic
if (user.isAnonymous()) {
return BasicAuthAction.unauthorized(response());
} else {
// If you want the Git client showing your custom message to
// the user, the Content-Type should be exact "text/plain"
// without any parameter. For more details, see the code at
// https://github.com/git/git/commit/426e70d4a11ce3b4f70636d57c6a0ab16ae08a00#diff-eea0ad565ec5903a11b6023755d491cfR154
response().setHeader("Content-Type", "text/plain");
return forbidden(
String.format("'%s' has no permission to '%s/%s'.",
user.loginId, ownerName, projectName));
String contentType = "text/plain", message;
if (isCharsetAllowed(request().getHeader("User-Agent"))) {
contentType += ";charset=" + Config.getCharset();
message = Messages.get(
"git.error.permission", user.loginId, ownerName, projectName);
} else {
message = Messages.get(Lang.defaultLang(),
"git.error.permission", user.loginId, ownerName, projectName);
}
response().setHeader("Content-Type", contentType);
return forbidden(message);
}
}

Expand Down
1 change: 1 addition & 0 deletions conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ fork.help.message.2 = Once you fork a project, you can contribute your code to t
fork.help.title = Fork this project''s repository.
fork.original = Forked from
fork.redirect.exist = Move to the forked project
git.error.permission = ''{0}'' has no permission to ''{1}/{2}''.
issue.assignToAuthor = Assign to author
issue.assignToMe = Assign to me
issue.assignee = Assignee
Expand Down
1 change: 1 addition & 0 deletions conf/messages.ko-KR
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ fork.help.message.2 = 코드 보내기 기능을 사용하여 복사한 프로
fork.help.title = 프로젝트의 코드 저장소를 복사합니다.
fork.original = 원본 프로젝트
fork.redirect.exist = 이전에 만들어둔 포크 프로젝트로 이동합니다.
git.error.permission = ''{0}''님은 ''{1}/{2}'' 프로젝트에 대한 권한이 없습니다.
issue.assignToAuthor = 등록자에게 할당하기
issue.assignToMe = 나에게 할당하기
issue.assignee = 담당자
Expand Down

0 comments on commit 1e9b9ee

Please sign in to comment.